Background:
- I am writing a meteor app which initially populates a db with data retrieved via SOAP requests made to a API end point on a server somewhere.
- The initial request is a search via a search term I select. I get back a list of id's to records that match my search.
- Then I make another API request, but this time, for each of those records which I then store to my own db (only selected values, not all the data)
- If I have a list of search terms then the above is performed for each of these.
- To avoid 'callback hell' and because I though it was a good opportunity to learn something new I opted to use Promises ordered sequentially: which goes something like this: forEach searchTerm -> getTheResults.then.forEachResult->fetchRecord
- For short sequences of a 100 or so it worked fine but when it got up to 1000 it would hang. After speaking to uncle Google, I came across some threads about native Promises not being optimized in some way and the third party libraries that were faster. So I decided to try Bluebird, but before doing this I would test the assertion that it might make things faster.
Code The code below creates a sequence of 'sleep promises'. The idea was to swap out the Bluebird promise implementation and observe the time test takes to run over a sequence 5000 Promises long.
var Promise = require("bluebird"); // comment out for native implementation
function sleep(time) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
// process.stdout.write(".");
resolve(true);
}, time);
});
}
// the function is badly named, please ignore
function nativePromiseSequence(n) {
// dont know how this first line works
const arr = Array.apply(null, Array(n)).map(function () {});
return arr.reduce(function(sequence, e, i, a) {
return sequence.then(function() {
return sleep(1);
});
}, Promise.resolve());
}
The Test I am using 'chai-as-promised' for testing the promises.
it('Run 5000 promises in a chain', function() {
this.timeout(7000);
return nativePromiseSequence(5000).should.eventually.equal(true);
});
The Result Over a Promise chain of 5000 the test with Bluebird implementation completed about a second slower that Native promises
Have I made a mistake here or missed something?