Skip to content

Commit bd79c37

Browse files
apapirovskiMylesBorins
authored andcommitted
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. PR-URL: #17279 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 0db1f87 commit bd79c37

10 files changed

+148
-174
lines changed

benchmark/timers/immediate.js

+3-1
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

+2-2
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

+7-7
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

+1-1
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

+3-3
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

+3-3
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

+3-3
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

+3-3
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]);
+21-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
'use strict';
22
const common = require('../common.js');
33

4+
// The following benchmark measures setting up n * 1e6 timeouts,
5+
// which then get executed on the next uv tick
6+
47
const bench = common.createBenchmark(main, {
5-
thousands: [500],
8+
millions: [10],
69
});
710

811
function main(conf) {
9-
const iterations = +conf.thousands * 1e3;
10-
var count = 0;
12+
const iterations = +conf.millions * 1e6;
13+
let count = 0;
1114

12-
for (var i = 0; i < iterations; i++) {
13-
setTimeout(cb, 1);
14-
}
15-
16-
bench.start();
15+
// Function tracking on the hidden class in V8 can cause misleading
16+
// results in this benchmark if only a single function is used —
17+
// alternate between two functions for a fairer benchmark
1718

1819
function cb() {
1920
count++;
2021
if (count === iterations)
21-
bench.end(iterations / 1e3);
22+
bench.end(iterations / 1e6);
2223
}
24+
function cb2() {
25+
count++;
26+
if (count === iterations)
27+
bench.end(iterations / 1e6);
28+
}
29+
30+
for (var i = 0; i < iterations; i++) {
31+
setTimeout(i % 2 ? cb : cb2, 1);
32+
}
33+
34+
bench.start();
2335
}

0 commit comments

Comments
 (0)