1

There is a big array, say of length 100. Each array contains a subarray of 50+ items. (Kind of 2-D array)

Rendering everything at one stretch is time consuming and might take more browser memory. Decided to go ahead with chunking the data sets to 5 and render them. That is, chunk the data render then take the next chunk and render it. Based on an interval say of 1second.

How is this achieved in RxJS?

Tried:

Rx.Observable.range(0, 10).map(i => data[i]).subscribe(...)

This takes the first 10 records. So I wrote a recursive function changing the 10 to iterator + 10, but it takes a lot of time to render the array items. Tried using:

Rx.Observable.interval(0, 500).take(data.length).map(i => data[i]).subscribe(...)

This works fine. But this takes 1 to render per 500ms time space. I would like to club first and second. chunk by 10 use 500ms to render and take the next chunk.

Abhilash L R
  • 805
  • 1
  • 9
  • 24

1 Answers1

2

You could try to use buffer to chunk the array. This assumes that the process function is synchronous.

Rx.Observable.from(bigArray)
  .bufferWithCount(chunkSize)
  .do(process(chunk))

That said, if the process function is synchronous why use Rxjs at all? Split your array into chunks (Cf. Split array into chunks on how to do that) and execute the process function on each chunk. It is probably faster and takes less memory.

Community
  • 1
  • 1
user3743222
  • 18,345
  • 5
  • 69
  • 75
  • What is chunk in this case? Do I have to manually split the bigArray into the chunkSize? I don't get it, sorry! – Abhilash L R Jun 10 '16 at 18:28
  • Look at the documentation of `bufferWithCount`. It returns an observable whose values are arrays of size `chunkSize`. – user3743222 Jun 10 '16 at 18:33