📜 ⬆️ ⬇️

Check the speed of the promis

In this article I decided to post some rather interesting, in my opinion, results of a benchmark of our own production. This benchmark was created to find out the speed of native and bluebird promises.

image


The source code of the benchmark (option number 1):

var Promise = require('bluebird'); //            . var crypto = require('crypto'); var iterations = 1000000; var start = Date.now(); var arrayPromises = []; var promiseWrap = function () { return new Promise(function (resolve, reject) { crypto.randomBytes(256, (err, buf) => { if (err) reject(err); resolve(buf); }); }); }; for(var i = 0; i < iterations; i++){ arrayPromises.push(promiseWrap()); } if(arrayPromises.length === iterations){ Promise.all(arrayPromises).then(function (result) { var finish = Date.now()-start; console.log("  !"); console.log("  "+finish/1000+" ."); }); } 

')
The source code of the benchmark (option number 2):

 var Promise = require('bluebird'); //            . var crypto = require('crypto'); var iterations = 1000000; var start = Date.now(); var arrayPromises = []; var promiseWrap = function () { return new Promise(function (resolve, reject) { setTimeout(function () { resolve(Math.random(1)); },0) }); }; for(var i = 0; i < iterations; i++){ arrayPromises.push(promiseWrap()); } if(arrayPromises.length === iterations){ Promise.all(arrayPromises).then(function (result) { var finish = Date.now()-start; console.log("  !"); console.log("  "+finish/1000+" ."); }); } 


This benchmark as a result displays the time during which all promises are resolved. The results of the benchmark on my machine:

Lead time:
Native promises (option number 1 node v6.4.0) - 19.808 sec.
Bluebird promises (option number 1 node v6.4.0) - 9.654 sec.
Native promises (option number 1 node v6.5.0) - 19.957 sec.
Bluebird promises (option number 1 node v6.5.0) - 9.723 sec.
Native promises (option number 2 node v6.5.0) - 10.61 sec.
Bluebird promises (option number 2 node v6.5.0) - 2.208 sec.


Maximum value of allocated memory (rss):
Native promises (option number 2 node v6.5.0) - 1282 MB.
Bluebird promises (option number 2 node v6.5.0) - 601 MB.


The average value of the allocated memory (rss):
Native promises (option number 2 node v6.5.0) - 368 MB.
Bluebird promises (option number 2 node v6.5.0) - 297 MB.


Conclusion: bluebird promises work 2-5 times faster than native promises, and also require much less RAM.

If I am mistaken in some way, then a big request to inform in the comments. I would be very happy to hear from you any amendments.

» Link to bluebird library

Source: https://habr.com/ru/post/308944/


All Articles