Skip to content

Commit

Permalink
util: use faster -0 check
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mscdex authored and MylesBorins committed Oct 7, 2017
1 parent dec383a commit 5268414
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 5 additions & 1 deletion benchmark/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const bench = common.createBenchmark(main, {
'Error',
'Array',
'TypedArray',
'TypedArray_extra'
'TypedArray_extra',
'Number'
],
option: Object.keys(opts)
});
Expand Down Expand Up @@ -92,6 +93,9 @@ function main({ method, n, option }) {
obj[Symbol('baz')] = 5;
benchmark(n, obj, options);
break;
case 'Number':
benchmark(n, 0, options);
break;
default:
throw new Error(`Unsupported method "${method}"`);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,10 @@ function formatValue(ctx, value, recurseTimes, ln) {
}

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

0 comments on commit 5268414

Please sign in to comment.