Skip to content

Commit

Permalink
benchmark: add setImmediate() benchmarks
Browse files Browse the repository at this point in the history
Timings for sequential and concurren setImmediate() with and without
arguments, and set + clearImmediate().

PR-URL: #6436
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
Andras authored and Fishrock123 committed Jul 5, 2016
1 parent efb7a90 commit 44f0f94
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions benchmark/timers/immediate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict';
var common = require('../common.js');

var bench = common.createBenchmark(main, {
thousands: [2000],
type: ['depth', 'depth1', 'breadth', 'breadth1', 'breadth4', 'clear']
});

function main(conf) {
var N = +conf.thousands * 1e3;
switch (conf.type) {
case 'depth':
depth(N);
break;
case 'depth1':
depth1(N);
break;
case 'breadth':
breadth(N);
break;
case 'breadth1':
breadth1(N);
break;
case 'breadth4':
breadth4(N);
break;
case 'clear':
clear(N);
break;
}
}

// setImmediate tail recursion, 0 arguments
function depth(N) {
var n = 0;
bench.start();
setImmediate(cb);
function cb() {
n++;
if (n === N)
bench.end(N / 1e3);
else
setImmediate(cb);
}
}

// setImmediate tail recursion, 1 argument
function depth1(N) {
var n = 0;
bench.start();
setImmediate(cb, 1);
function cb(a1) {
n++;
if (n === N)
bench.end(N / 1e3);
else
setImmediate(cb, 1);
}
}

// concurrent setImmediate, 0 arguments
function breadth(N) {
var n = 0;
bench.start();
function cb() {
n++;
if (n === N)
bench.end(N / 1e3);
}
for (var i = 0; i < N; i++) {
setImmediate(cb);
}
}

// concurrent setImmediate, 1 argument
function breadth1(N) {
var n = 0;
bench.start();
function cb(a1) {
n++;
if (n === N)
bench.end(N / 1e3);
}
for (var i = 0; i < N; i++) {
setImmediate(cb, 1);
}
}

// concurrent setImmediate, 4 arguments
function breadth4(N) {
var n = 0;
bench.start();
function cb(a1, a2, a3, a4) {
n++;
if (n === N)
bench.end(N / 1e3);
}
for (var i = 0; i < N; i++) {
setImmediate(cb, 1, 2, 3, 4);
}
}

function clear(N) {
bench.start();
function cb(a1) {
if (a1 === 2)
bench.end(N / 1e3);
}
for (var i = 0; i < N; i++) {
clearImmediate(setImmediate(cb, 1));
}
setImmediate(cb, 2);
}

0 comments on commit 44f0f94

Please sign in to comment.