Skip to content

Commit a77330e

Browse files
committed
timers: clean up for readability
Remove micro-optimizations that no longer yield any benefits, restructure timers & immediates to be a bit more straightforward. Adjust timers benchmarks to run long enough to offer meaningful data.
1 parent 3a4fe77 commit a77330e

10 files changed

+145
-175
lines changed

benchmark/timers/immediate.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
5-
thousands: [2000],
5+
thousands: [5000],
66
type: ['depth', 'depth1', 'breadth', 'breadth1', 'breadth4', 'clear']
77
});
88

@@ -88,6 +88,7 @@ function breadth1(N) {
8888

8989
// concurrent setImmediate, 4 arguments
9090
function breadth4(N) {
91+
N /= 2;
9192
var n = 0;
9293
bench.start();
9394
function cb(a1, a2, a3, a4) {
@@ -101,6 +102,7 @@ function breadth4(N) {
101102
}
102103

103104
function clear(N) {
105+
N *= 4;
104106
bench.start();
105107
function cb(a1) {
106108
if (a1 === 2)

benchmark/timers/set-immediate-breadth-args.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function main(conf) {
1919
bench.start();
2020
for (let i = 0; i < N; i++) {
2121
if (i % 3 === 0)
22-
setImmediate(cb3, 512, true, null);
22+
setImmediate(cb3, 512, true, null, 512, true, null);
2323
else if (i % 2 === 0)
24-
setImmediate(cb2, false, 5.1);
24+
setImmediate(cb2, false, 5.1, 512);
2525
else
2626
setImmediate(cb1, 0);
2727
}

benchmark/timers/set-immediate-depth-args.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const common = require('../common.js');
44
const bench = common.createBenchmark(main, {
5-
millions: [10]
5+
millions: [5]
66
});
77

88
function main(conf) {
@@ -15,29 +15,29 @@ function main(conf) {
1515
function cb3(n, arg2, arg3) {
1616
if (--n) {
1717
if (n % 3 === 0)
18-
setImmediate(cb3, n, true, null);
18+
setImmediate(cb3, n, true, null, 5.1, null, true);
1919
else if (n % 2 === 0)
20-
setImmediate(cb2, n, 5.1);
20+
setImmediate(cb2, n, 5.1, true);
2121
else
2222
setImmediate(cb1, n);
2323
}
2424
}
2525
function cb2(n, arg2) {
2626
if (--n) {
2727
if (n % 3 === 0)
28-
setImmediate(cb3, n, true, null);
28+
setImmediate(cb3, n, true, null, 5.1, null, true);
2929
else if (n % 2 === 0)
30-
setImmediate(cb2, n, 5.1);
30+
setImmediate(cb2, n, 5.1, true);
3131
else
3232
setImmediate(cb1, n);
3333
}
3434
}
3535
function cb1(n) {
3636
if (--n) {
3737
if (n % 3 === 0)
38-
setImmediate(cb3, n, true, null);
38+
setImmediate(cb3, n, true, null, 5.1, null, true);
3939
else if (n % 2 === 0)
40-
setImmediate(cb2, n, 5.1);
40+
setImmediate(cb2, n, 5.1, true);
4141
else
4242
setImmediate(cb1, n);
4343
}

benchmark/timers/timers-breadth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
5-
thousands: [500],
5+
thousands: [5000],
66
});
77

88
function main(conf) {

benchmark/timers/timers-cancel-pooled.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const common = require('../common.js');
33
const assert = require('assert');
44

55
const bench = common.createBenchmark(main, {
6-
thousands: [500],
6+
millions: [5],
77
});
88

99
function main(conf) {
10-
const iterations = +conf.thousands * 1e3;
10+
const iterations = +conf.millions * 1e6;
1111

1212
var timer = setTimeout(() => {}, 1);
1313
for (var i = 0; i < iterations; i++) {
@@ -24,7 +24,7 @@ function main(conf) {
2424
clearTimeout(timer);
2525
}
2626

27-
bench.end(iterations / 1e3);
27+
bench.end(iterations / 1e6);
2828
}
2929

3030
function cb() {

benchmark/timers/timers-cancel-unpooled.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const common = require('../common.js');
33
const assert = require('assert');
44

55
const bench = common.createBenchmark(main, {
6-
thousands: [100],
6+
millions: [1],
77
});
88

99
function main(conf) {
10-
const iterations = +conf.thousands * 1e3;
10+
const iterations = +conf.millions * 1e6;
1111

1212
const timersList = [];
1313
for (var i = 0; i < iterations; i++) {
@@ -18,7 +18,7 @@ function main(conf) {
1818
for (var j = 0; j < iterations + 1; j++) {
1919
clearTimeout(timersList[j]);
2020
}
21-
bench.end(iterations / 1e3);
21+
bench.end(iterations / 1e6);
2222
}
2323

2424
function cb() {

benchmark/timers/timers-insert-pooled.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
5-
thousands: [500],
5+
millions: [5],
66
});
77

88
function main(conf) {
9-
const iterations = +conf.thousands * 1e3;
9+
const iterations = +conf.millions * 1e6;
1010

1111
bench.start();
1212

1313
for (var i = 0; i < iterations; i++) {
1414
setTimeout(() => {}, 1);
1515
}
1616

17-
bench.end(iterations / 1e3);
17+
bench.end(iterations / 1e6);
1818
}

benchmark/timers/timers-insert-unpooled.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ const common = require('../common.js');
33
const assert = require('assert');
44

55
const bench = common.createBenchmark(main, {
6-
thousands: [100],
6+
millions: [1],
77
});
88

99
function main(conf) {
10-
const iterations = +conf.thousands * 1e3;
10+
const iterations = +conf.millions * 1e6;
1111

1212
const timersList = [];
1313

1414
bench.start();
1515
for (var i = 0; i < iterations; i++) {
1616
timersList.push(setTimeout(cb, i + 1));
1717
}
18-
bench.end(iterations / 1e3);
18+
bench.end(iterations / 1e6);
1919

2020
for (var j = 0; j < iterations + 1; j++) {
2121
clearTimeout(timersList[j]);

benchmark/timers/timers-timeout-pooled.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22
const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
5-
thousands: [500],
5+
millions: [10],
66
});
77

88
function main(conf) {
9-
const iterations = +conf.thousands * 1e3;
10-
var count = 0;
9+
const iterations = +conf.millions * 1e6;
10+
11+
// Function tracking on the hidden class in V8 can cause misleading
12+
// results in this benchmark if only a single function is used —
13+
// alternate between two functions for a fairer benchmark
14+
15+
function cb() {}
16+
function cb2() {}
17+
18+
process.on('exit', function() {
19+
bench.end(iterations / 1e6);
20+
});
1121

1222
for (var i = 0; i < iterations; i++) {
13-
setTimeout(cb, 1);
23+
setTimeout(i % 2 ? cb : cb2, 1);
1424
}
1525

1626
bench.start();
17-
18-
function cb() {
19-
count++;
20-
if (count === iterations)
21-
bench.end(iterations / 1e3);
22-
}
2327
}

0 commit comments

Comments
 (0)