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‎

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

44
constbench=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
functionbreadth4(N){
91+
N/=2;
9192
varn=0;
9293
bench.start();
9394
functioncb(a1,a2,a3,a4){
@@ -101,6 +102,7 @@ function breadth4(N){
101102
}
102103

103104
functionclear(N){
105+
N*=4;
104106
bench.start();
105107
functioncb(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(leti=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
elseif(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
constcommon=require('../common.js');
44
constbench=common.createBenchmark(main,{
5-
millions: [10]
5+
millions: [5]
66
});
77

88
functionmain(conf){
@@ -15,29 +15,29 @@ function main(conf){
1515
functioncb3(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
elseif(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
functioncb2(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
elseif(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
functioncb1(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
elseif(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
constcommon=require('../common.js');
33

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

88
functionmain(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
constassert=require('assert');
44

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

99
functionmain(conf){
10-
constiterations=+conf.thousands*1e3;
10+
constiterations=+conf.millions*1e6;
1111

1212
vartimer=setTimeout(()=>{},1);
1313
for(vari=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
functioncb(){

‎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
constassert=require('assert');
44

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

99
functionmain(conf){
10-
constiterations=+conf.thousands*1e3;
10+
constiterations=+conf.millions*1e6;
1111

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

2424
functioncb(){

‎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
constcommon=require('../common.js');
33

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

88
functionmain(conf){
9-
constiterations=+conf.thousands*1e3;
9+
constiterations=+conf.millions*1e6;
1010

1111
bench.start();
1212

1313
for(vari=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
constassert=require('assert');
44

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

99
functionmain(conf){
10-
constiterations=+conf.thousands*1e3;
10+
constiterations=+conf.millions*1e6;
1111

1212
consttimersList=[];
1313

1414
bench.start();
1515
for(vari=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(varj=0;j<iterations+1;j++){
2121
clearTimeout(timersList[j]);
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
'use strict';
22
constcommon=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
constbench=common.createBenchmark(main,{
5-
thousands: [500],
8+
millions: [10],
69
});
710

811
functionmain(conf){
9-
constiterations=+conf.thousands*1e3;
10-
varcount=0;
12+
constiterations=+conf.millions*1e6;
13+
letcount=0;
1114

12-
for(vari=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
functioncb(){
1920
count++;
2021
if(count===iterations)
21-
bench.end(iterations/1e3);
22+
bench.end(iterations/1e6);
2223
}
24+
functioncb2(){
25+
count++;
26+
if(count===iterations)
27+
bench.end(iterations/1e6);
28+
}
29+
30+
for(vari=0;i<iterations;i++){
31+
setTimeout(i%2 ? cb : cb2,1);
32+
}
33+
34+
bench.start();
2335
}

0 commit comments

Comments
(0)