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 <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 170e196 commit f79cf70

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

Diff for: benchmark/process/bench-hrtime.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,38 @@ const assert = require('assert');
55

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

1111
function main({ n, type }) {
1212
const hrtime = process.hrtime;
13-
var noDead = hrtime();
13+
var noDead = type === 'bigint' ? hrtime.bigint() : hrtime();
1414
var i;
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) || typeof noDead === 'bigint');
3142
}

Diff for: lib/internal/process/per_thread.js

+2-2
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
const hrBigintValues = new BigUint64Array(1);
128128
function hrtimeBigInt() {
129129
_hrtimeBigInt(hrBigintValues);

0 commit comments

Comments
 (0)