Skip to content

Commit 58c68c2

Browse files
mscdexMylesBorins
authored andcommitted
util: use faster -0 check
PR-URL: #15726 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Benedikt Meurer <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent 6003afc commit 58c68c2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

benchmark/util/inspect.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const bench = common.createBenchmark(main, {
2222
'Error',
2323
'Array',
2424
'TypedArray',
25-
'TypedArray_extra'
25+
'TypedArray_extra',
26+
'Number'
2627
],
2728
option: Object.keys(opts)
2829
});
@@ -92,6 +93,9 @@ function main({ method, n, option }) {
9293
obj[Symbol('baz')] = 5;
9394
benchmark(n, obj, options);
9495
break;
96+
case 'Number':
97+
benchmark(n, 0, options);
98+
break;
9599
default:
96100
throw new Error(`Unsupported method "${method}"`);
97101
}

lib/util.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,10 @@ function formatValue(ctx, value, recurseTimes, ln) {
614614
}
615615

616616
function formatNumber(fn, value) {
617-
// Format -0 as '-0'. Strict equality won't distinguish 0 from -0.
618-
if (Object.is(value, -0))
617+
// Format -0 as '-0'. A `value === -0` check won't distinguish 0 from -0.
618+
// Using a division check is currently faster than `Object.is(value, -0)`
619+
// as of V8 6.1.
620+
if (1 / value === -Infinity)
619621
return fn('-0', 'number');
620622
return fn(`${value}`, 'number');
621623
}

0 commit comments

Comments
 (0)