From 58c68c2fcb37984893f3ff62991a18f5d8e04628 Mon Sep 17 00:00:00 2001 From: Brian White Date: Mon, 2 Oct 2017 02:58:24 -0400 Subject: [PATCH] util: use faster -0 check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/15726 Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Jeremiah Senkpiel Reviewed-By: Tobias Nießen Reviewed-By: Benedikt Meurer Reviewed-By: Yuta Hiroto --- benchmark/util/inspect.js | 6 +++++- lib/util.js | 6 ++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/benchmark/util/inspect.js b/benchmark/util/inspect.js index 913850690ce655..35253ac96682eb 100644 --- a/benchmark/util/inspect.js +++ b/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}"`); } diff --git a/lib/util.js b/lib/util.js index c12d80de34086b..6fdae22c706b6c 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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'); }