Browse Source

util: use faster -0 check

PR-URL: https://github.com/nodejs/node/pull/15726
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
v9.x-staging
Brian White 7 years ago
parent
commit
d545c948c2
No known key found for this signature in database GPG Key ID: 606D7358F94DA209
  1. 6
      benchmark/util/inspect.js
  2. 6
      lib/util.js

6
benchmark/util/inspect.js

@ -22,7 +22,8 @@ const bench = common.createBenchmark(main, {
'Error',
'Array',
'TypedArray',
'TypedArray_extra'
'TypedArray_extra',
'Number'
],
option: Object.keys(opts)
});
@ -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}"`);
}

6
lib/util.js

@ -615,8 +615,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');
}

Loading…
Cancel
Save