Skip to content

Instantly share code, notes, and snippets.

@bluejava
Created April 7, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluejava/b6fb511abd2c3479c4db4246f694b831 to your computer and use it in GitHub Desktop.
Save bluejava/b6fb511abd2c3479c4db4246f694b831 to your computer and use it in GitHub Desktop.
Runs some simple performance tests on Node for Native Promises, Zousan and Bluebird. (See Issue 5 : https://github.com/bluejava/zousan/issues/5)
var batchSize = 1000,
doBluebirdAll = true,
doZousanAll = true,
doNativeAll = true,
doBluebirdSeries = true,
doZousanSeries = true,
doNativeSeries = true
console.log(" - Batch Size: " + batchSize)
var Zousan = require("zousan"),
Bluebird = require('bluebird'),
promiseName = null,
PromiseImpl = null,
bp = Zousan.resolve(1)
if(doBluebirdAll)
bp = bp.then(function() {
promiseName = "Bluebird-All"
PromiseImpl = Bluebird
return testAll()
.then(testAll)
.then(testAll)
.then(testAll)
.then(testAll)
})
if(doZousanAll)
bp = bp.then(function() {
promiseName = "Zousan-All"
PromiseImpl = Zousan
return testAll()
.then(testAll)
.then(testAll)
.then(testAll)
.then(testAll)
})
if(doNativeAll)
bp = bp.then(function() {
promiseName = "Native-All"
PromiseImpl = Promise
return testAll()
.then(testAll)
.then(testAll)
.then(testAll)
.then(testAll)
})
if(doBluebirdSeries)
bp = bp.then(function() {
promiseName = "Bluebird-Series"
PromiseImpl = Bluebird
return testSeries()
.then(testSeries)
.then(testSeries)
.then(testSeries)
.then(testSeries)
})
if(doZousanSeries)
bp = bp.then(function() {
promiseName = "Zousan-Series"
PromiseImpl = Zousan
return testSeries()
.then(testSeries)
.then(testSeries)
.then(testSeries)
.then(testSeries)
})
if(doNativeSeries)
bp = bp.then(function() {
promiseName = "Native-Series"
PromiseImpl = Promise
return testSeries()
.then(testSeries)
.then(testSeries)
.then(testSeries)
.then(testSeries)
})
bp.then(showAverages)
// ---------------------- The Test Routines ------------------
function testAll()
{
var items = [];
timeStart(promiseName);
for(var i = 0; i < batchSize; i++) {
items.push(new PromiseImpl(function(resolve, reject) {
resolve({time: process.hrtime()} )
}));
}
var done = PromiseImpl.all(items)
done.then((res) => { timeEnd(promiseName) })
return done
}
function testSeries()
{
timeStart(promiseName);
var p = testSeries2(0)
p.then(() => { timeEnd(promiseName) })
return p
}
function testSeries2(i)
{
if(i == batchSize)
return PromiseImpl.resolve(1)
return new PromiseImpl(function(resolve, reject) {
resolve(i)
}).then(function() { return testSeries2(i+1) })
}
// ---------------------- Some Utility Functions ------------------
function log() {
for(a in arguments) console.log(arguments[a])
}
var current = {}, history = {}
function timeStart(label)
{
console.log("Starting " + label)
current[label] =process.hrtime()
}
function timeEnd(label)
{
var diff = process.hrtime(current[label]),
totalTime = (diff[0] * 1e9 + diff[1]) / 1e6,
historyItem = history[label]
if(!historyItem)
{
historyItem = { reps: 0, total: 0 }
history[label] = historyItem
}
historyItem.reps++
historyItem.total += totalTime
console.log(label + ": " + totalTime + "ms")
}
function showAverages()
{
for(label in history)
console.log(label + " - Average runtime for " + history[label].reps + " repititions = " + Math.round(history[label].total * 100 / history[label].reps) / 100 + "ms")
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment