javascript - Need help understanding performance of native promises versus Blubird -
background:
- i writing meteor app populates db data retrieved via soap requests made api end point on server somewhere.
- the initial request search via search term select. list of id's records match search.
- then make api request, time, each of records store own db (only selected values, not data)
- if have list of search terms above performed each of these.
- to avoid 'callback hell' , because though opportunity learn new opted use promises ordered sequentially: goes this: foreach searchterm -> gettheresults.then.foreachresult->fetchrecord
- for short sequences of 100 or worked fine when got 1000 hang. after speaking uncle google, came across threads native promises not being optimized in way , third party libraries faster. decided try bluebird, before doing test assertion might make things faster.
code code below creates sequence of 'sleep promises'. idea swap out bluebird promise implementation , observe time test takes run on sequence 5000 promises long.
var promise = require("bluebird"); // comment out native implementation function sleep(time) { return new promise(function (resolve, reject) { settimeout(function() { // process.stdout.write("."); resolve(true); }, time); }); } // function badly named, please ignore function nativepromisesequence(n) { // dont know how 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 using 'chai-as-promised' testing promises.
it('run 5000 promises in chain', function() { this.timeout(7000); return nativepromisesequence(5000).should.eventually.equal(true); });
the result on promise chain of 5000 test bluebird implementation completed second slower native promises
have made mistake here or missed something?
Comments
Post a Comment