title: Sequencing in RxJS results in "out of memory" error
body: <p>I need to process 1 million lines of record, transform each one of them, and save them to multiple files (binned by the hour; 1 file for each hour -- I'm thinking of splitting them by "filter").</p>
  
  <p>For some reason I need those lines to be processed strictly sequentially. Meaning, if line #450000 takes longer to process and to save (this is the tricky part because fs is async with callback), the processing wouldn't jump to #450001... It will wait until 450000 is finished. That random sleeping in the code is to simulate that scenario.</p>
  
  <p>Previously (with simple Promise, no RxJs), I would create N promises, one for each line, keep them in an array, and do the chaining by reduce op, as explained here: <a href="https://github.com/kriskowal/q" rel="nofollow noreferrer">https://github.com/kriskowal/q</a></p>
  
  <p>But I don't want to create 1 million instances of Promises. So, I looked into ReactiveX, hoping that it will be like "pass the buck"; meaning it wouldn't wait, the processing would take place as soon as an event pops-up, and the resource used by the processing (thinking that the processing block is basically a promise behind the scene) will be released as soon as possible.</p>
  
  <p>I tried to verify that with this code:</p>
  
  <pre><code>import Rx from 'rxjs-es6/Rx';
  import Q from 'q';    
  
  let subject = new Rx.Subject();
  let processEventJsons = function(observable) {
    observable.concatMap(eventJson =&gt; {
      let deferred = Q.defer();    
  
      setTimeout(() =&gt; {
        eventJson.procDatetime = new Date().toISOString();
        deferred.resolve(eventJson);
      }, Math.random() * 5000);    
  
      return Rx.Observable.fromPromise(deferred.promise)
    })
    .subscribe({
      next: enrichedEventJson =&gt; {
        console.log(JSON.stringify(enrichedEventJson));
      },
      error: err =&gt; console.error('something wrong occurred: ' + err),
      complete: () =&gt; console.log('done'),
    });
  }    
  
  processEventJsons(
    subject.filter(dataJson =&gt; dataJson.type === "interview").map(dataJson =&gt; {
      return {event: "intv", datetime: dataJson.datetime}
    })
  )    
  
  processEventJsons(
    subject.filter(dataJson =&gt; dataJson.type === "checkin").map(dataJson =&gt; {
      return {event: "chki", datetime: dataJson.datetime}
    })
  )    
  
  for (let i = 0; i &lt; 1000000; i++) {
    if (Math.random() &lt; 0.5) {
      subject.next({id: i, type: "interview", datetime: new Date().toISOString()});
    } else {
      subject.next({id: i, type: "checkin", datetime: new Date().toISOString()});
    }
  }
  subject.complete();
  </code></pre>
  
  <p>But... I kept getting:</p>
  
  <blockquote>
    <p>FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory.</p>
  </blockquote>
  
  <p>The console.log(JSON.stringify(enrichedEventJson)); doesn't get printed until the "for-loop" (at the end of the code) is completed.</p>
  
  <p>This makes me think that switching to RxJS hasn't really improved the situation; it still queues-up promises behind the scene.</p>
  
  <p>Or am I using the API wrongly? Can you help me point out what's wrong?</p>
  
  <h2>UPDATE UPDATE:</h2>
  
  <p>False Flag. Figured out the problem is not the in the use of RxJS, but in the for-loop (it's too tight). SO I changed it to:</p>
  
  <pre><code>for (let i = 0; i &lt; 1000000; i++) {
    if (Math.random() &lt; 0.5) {
      setTimeout(() =&gt; {
        subject.next({id: i, type: "interview", datetime: new Date().toISOString()});
      });
    } else {
      setTimeout(() =&gt; {
        subject.next({id: i, type: "checkin", datetime: new Date().toISOString()});
      });
    }
  }
  </code></pre>