DEV Community

Cover image for RxJS Is Just Arrays Over Time
Amrishkhan Sheik Abdullah
Amrishkhan Sheik Abdullah

Posted on

RxJS Is Just Arrays Over Time

RxJS has a reputation problem.

Mention RxJS in a JavaScript discussion and you'll usually get one of two reactions.

The first:

I love RxJS.
Enter fullscreen mode Exit fullscreen mode

The second:

I have no idea what's going on.
Enter fullscreen mode Exit fullscreen mode

There is rarely an in-between.

And honestly, I understand why.

Most RxJS tutorials immediately throw developers into a sea of operators:

map
filter
switchMap
mergeMap
concatMap
combineLatest
forkJoin
zip
scan
Enter fullscreen mode Exit fullscreen mode

It feels like learning an entirely new programming language.

But what if I told you that most RxJS operators are concepts you already know?

What if the problem isn't RxJS?

What if the problem is how RxJS is taught?

Because once I understood one simple idea, RxJS suddenly became much easier:

RxJS is just Arrays over Time.

Not exactly.

But close enough that it dramatically simplifies how you think about Observables.


Start With Something Familiar

Consider an Array.

const numbers = [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

We can transform it.

const doubled = numbers.map(
  x => x * 2
)

console.log(doubled)
Enter fullscreen mode Exit fullscreen mode

Output:

[2, 4, 6, 8]
Enter fullscreen mode Exit fullscreen mode

We can filter it.

const even = numbers.filter(
  x => x % 2 === 0
)
Enter fullscreen mode Exit fullscreen mode

Output:

[2, 4]
Enter fullscreen mode Exit fullscreen mode

We can reduce it.

const total = numbers.reduce(
  (sum, x) => sum + x,
  0
)
Enter fullscreen mode Exit fullscreen mode

Output:

10
Enter fullscreen mode Exit fullscreen mode

Nothing surprising.

Every JavaScript developer understands this.


Now Imagine The Values Arrive Over Time

Instead of:

[1,2,3,4]
Enter fullscreen mode Exit fullscreen mode

imagine:

1
(wait)
2
(wait)
3
(wait)
4
Enter fullscreen mode Exit fullscreen mode

The values are the same.

The only difference is:

Space
↓

Time
Enter fullscreen mode Exit fullscreen mode

That is the key insight.


Enter Observables

An Observable might emit:

1
2
3
4
Enter fullscreen mode Exit fullscreen mode

over time.

Example:

import {
  interval
} from "rxjs"

interval(1000)
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
...
Enter fullscreen mode Exit fullscreen mode

One value every second.

Instead of holding values simultaneously like an Array, an Observable delivers values gradually.


map() Works Exactly The Same

Array:

[1, 2, 3]
  .map(x => x * 2)
Enter fullscreen mode Exit fullscreen mode

Observable:

interval(1000)
  .pipe(
    map(x => x * 2)
  )
Enter fullscreen mode Exit fullscreen mode

Output:

0
2
4
6
8
...
Enter fullscreen mode Exit fullscreen mode

The transformation is identical.

Only the timing differs.


filter() Works Exactly The Same

Array:

numbers.filter(
  x => x % 2 === 0
)
Enter fullscreen mode Exit fullscreen mode

Observable:

interval(1000)
  .pipe(
    filter(
      x => x % 2 === 0
    )
  )
Enter fullscreen mode Exit fullscreen mode

Output:

0
2
4
6
8
...
Enter fullscreen mode Exit fullscreen mode

Again:

Same operation.

Different delivery mechanism.


scan() Is Reduce For Infinite Data

This is where many RxJS developers finally have their "aha" moment.

Arrays use:

reduce()
Enter fullscreen mode Exit fullscreen mode

Example:

[1, 2, 3, 4]
  .reduce(
    (sum, x) => sum + x,
    0
  )
Enter fullscreen mode Exit fullscreen mode

Output:

10
Enter fullscreen mode Exit fullscreen mode

But Observables can be infinite.

They never finish.

So:

reduce()
Enter fullscreen mode Exit fullscreen mode

often doesn't make sense.

Instead RxJS gives us:

scan()
Enter fullscreen mode Exit fullscreen mode

Example:

interval(1000)
  .pipe(
    scan(
      (sum, x) => sum + x,
      0
    )
  )
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
3
6
10
15
21
...
Enter fullscreen mode Exit fullscreen mode

This is:

Reduce
Over Time
Enter fullscreen mode Exit fullscreen mode

And once you see that, scan becomes incredibly intuitive.


flatMap Exists Here Too

In the previous article we discussed:

flatMap()
Enter fullscreen mode Exit fullscreen mode

Arrays:

users.flatMap(
  user => user.permissions
)
Enter fullscreen mode Exit fullscreen mode

RxJS:

searchText$
  .pipe(
    mergeMap(
      text =>
        api.search(text)
    )
  )
Enter fullscreen mode Exit fullscreen mode

Same problem.

Same solution.

The difference is:

Arrays
↓

Nested Collections
Enter fullscreen mode Exit fullscreen mode

versus

Observables
↓

Nested Streams
Enter fullscreen mode Exit fullscreen mode

switchMap, mergeMap and concatMap

These operators confuse almost everyone initially.

The reason is simple.

Arrays don't have time.

Observables do.

Suppose:

Search A
Search B
Search C
Enter fullscreen mode Exit fullscreen mode

happen rapidly.

Now:

Which API calls should survive?
Enter fullscreen mode Exit fullscreen mode

That is what these operators answer.


mergeMap

Run everything.

A
B
C

All continue.
Enter fullscreen mode Exit fullscreen mode

concatMap

Queue everything.

A
↓

B
↓

C
Enter fullscreen mode Exit fullscreen mode

switchMap

Cancel old work.

A ❌

B ❌

C ✅
Enter fullscreen mode Exit fullscreen mode

Perfect for search boxes.


Why RxJS Feels Hard

Because developers often memorize operators.

Instead of understanding patterns.

If you think:

map
filter
scan
flatMap
Enter fullscreen mode Exit fullscreen mode

then RxJS becomes much simpler.

Because you've already seen these concepts before.

In Arrays.


Real World Example: Search Autocomplete

User types:

a
ap
app
appl
apple
Enter fullscreen mode Exit fullscreen mode

Without RxJS:

You manually manage:

  • timers
  • cancellation
  • race conditions

With RxJS:

search$
  .pipe(
    debounceTime(300),
    distinctUntilChanged(),
    switchMap(
      query =>
        api.search(query)
    )
  )
Enter fullscreen mode Exit fullscreen mode

This is why RxJS exists.

It turns asynchronous complexity into composable transformations.


Real World Example: WebSocket Streams

Messages arrive continuously.

Message 1
Message 2
Message 3
Enter fullscreen mode Exit fullscreen mode

Treating them as a stream allows:

messages$
  .pipe(
    filter(
      msg => msg.priority
    ),
    map(
      msg => msg.payload
    )
  )
Enter fullscreen mode Exit fullscreen mode

Exactly like Arrays.

Except the values arrive over time.


Performance Considerations

Arrays:

array
  .map(...)
  .filter(...)
Enter fullscreen mode Exit fullscreen mode

may create intermediate arrays.

RxJS:

stream
  .pipe(
    map(...),
    filter(...)
  )
Enter fullscreen mode Exit fullscreen mode

usually processes values incrementally.

One item at a time.

This can significantly reduce memory pressure for large streams.


Pros Of Thinking This Way

1. RxJS Becomes Easier

Most operators stop feeling magical.


2. Reuse Existing Knowledge

Array knowledge transfers directly.


3. Better Mental Models

Patterns become obvious.


4. Easier Debugging

You stop treating operators as black boxes.


5. Strong Foundation For Reactive Programming

Everything else builds on these concepts.


Cons Of The Analogy

1. Arrays Are Finite

Observables may be infinite.


2. Time Changes Everything

Cancellation becomes important.


3. Concurrency Doesn't Exist In Arrays

RxJS operators must handle it.


4. Error Handling Differs

Streams behave differently from collections.


5. The Analogy Eventually Breaks

At advanced levels.

But it remains extremely useful.


The Real Lesson

The biggest mistake people make with RxJS is treating it as something completely new.

It isn't.

Most of its core ideas already exist in JavaScript.

You've seen:

map()
filter()
reduce()
flatMap()
Enter fullscreen mode Exit fullscreen mode

before.

RxJS simply applies those ideas to values that arrive over time.

And once you realize that:

Observable

≈

Array Over Time
Enter fullscreen mode Exit fullscreen mode

the entire library becomes much less intimidating.

Not because the complexity disappeared.

But because you finally have a mental model that makes sense.


What's Next?

In the next article we'll revisit one of the most misunderstood operators in RxJS:

scan() Is Reduce For Infinite Data

Because understanding scan() unlocks:

  • RxJS state management
  • Redux-like patterns
  • Event sourcing
  • Reactive architectures

and ultimately connects everything back to the humble reduce() function where this series began.


About The Author

Hi, I'm Amrish Khan.

I enjoy building developer tools, exploring software architecture, and writing about the deeper ideas behind everyday programming concepts.

I'm also building Aruvix — a growing ecosystem of local-first developer tools designed to process data directly in the browser without unnecessary uploads.

Here's a detailed blog on Aruvix:

https://dev.to/amrishkhan05/aruvix-the-ultimate-offline-first-developer-toolkit-e0i

You can follow my work and thoughts here:

Portfolio:
https://www.amrishkhan.dev

LinkedIn:
https://www.linkedin.com/in/amrishkhan

GitHub:
https://www.github.com/amrishkhan05

If you enjoyed this article, consider following for more deep dives into JavaScript, architecture, local-first software, and performance engineering.

Top comments (0)