Skip to content

Commit f79cf70

Browse files
addaleaxBridgeAR
authored andcommitted
benchmark,lib: add process.hrtime.bigint benchmark
Add a benchmark, and amend the relevant source code comment to state that currently, switching to directly returning a BigInt is not stopped by technical obstacles but rather the fact that using a typed array is actually a bit faster (about 2.5 %, measured locally). PR-URL: #26381 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 170e196 commit f79cf70

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

‎benchmark/process/bench-hrtime.js‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,38 @@ const assert = require('assert');
55

66
constbench=common.createBenchmark(main,{
77
n: [1e6],
8-
type: ['raw','diff']
8+
type: ['raw','diff','bigint']
99
});
1010

1111
functionmain({ n, type }){
1212
consthrtime=process.hrtime;
13-
varnoDead=hrtime();
13+
varnoDead=type==='bigint' ? hrtime.bigint() : hrtime();
1414
vari;
1515

16-
if(type==='raw'){
17-
bench.start();
18-
for(i=0;i<n;i++){
19-
noDead=hrtime();
20-
}
21-
bench.end(n);
22-
}else{
23-
bench.start();
24-
for(i=0;i<n;i++){
25-
noDead=hrtime(noDead);
26-
}
27-
bench.end(n);
16+
switch(type){
17+
case'raw':
18+
bench.start();
19+
for(i=0;i<n;i++){
20+
noDead=hrtime();
21+
}
22+
bench.end(n);
23+
break;
24+
case'diff':
25+
bench.start();
26+
for(i=0;i<n;i++){
27+
noDead=hrtime(noDead);
28+
}
29+
bench.end(n);
30+
break;
31+
case'bigint':
32+
bench.start();
33+
for(i=0;i<n;i++){
34+
noDead=hrtime.bigint();
35+
}
36+
bench.end(n);
37+
break;
2838
}
2939

30-
assert.ok(Array.isArray(noDead));
40+
// eslint-disable-next-line valid-typeof
41+
assert.ok(Array.isArray(noDead)||typeofnoDead==='bigint');
3142
}

‎lib/internal/process/per_thread.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ function wrapProcessMethods(binding) {
122122
];
123123
}
124124

125-
// Use a BigUint64Array in the closure because V8 does not have an API for
126-
// creating a BigInt out of a uint64_t yet.
125+
// Use a BigUint64Array in the closure because this is actually a bit
126+
// faster than simply returning a BigInt from C++ in V8 7.1.
127127
consthrBigintValues=newBigUint64Array(1);
128128
functionhrtimeBigInt(){
129129
_hrtimeBigInt(hrBigintValues);

0 commit comments

Comments
 (0)