From 8039ca06eb808b707313da37b9815288f50e5a24 Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Sun, 22 Nov 2015 17:08:45 +0800 Subject: [PATCH] util: faster arrayToHash The `util.format()` is used frequently, make the method faster is better. R-URL: https://github.com/nodejs/node/pull/3964 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: Brian White Reviewed-By: James M Snell --- Makefile | 3 +++ benchmark/util/inspect.js | 15 +++++++++++++++ lib/util.js | 5 +++-- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 benchmark/util/inspect.js diff --git a/Makefile b/Makefile index a99b112508..94eb419953 100644 --- a/Makefile +++ b/Makefile @@ -493,6 +493,9 @@ bench-url: all bench-events: all @$(NODE) benchmark/common.js events +bench-util: all + @$(NODE) benchmark/common.js util + bench-all: bench bench-misc bench-array bench-buffer bench-url bench-events bench: bench-net bench-http bench-fs bench-tls diff --git a/benchmark/util/inspect.js b/benchmark/util/inspect.js new file mode 100644 index 0000000000..8a59e6b48e --- /dev/null +++ b/benchmark/util/inspect.js @@ -0,0 +1,15 @@ +var util = require('util'); + +var common = require('../common.js'); + +var bench = common.createBenchmark(main, {n: [5e6]}); + +function main(conf) { + var n = conf.n | 0; + + bench.start(); + for (var i = 0; i < n; i += 1) { + var r = util.inspect({a: 'a', b: 'b', c: 'c', d: 'd'}); + } + bench.end(n); +} diff --git a/lib/util.js b/lib/util.js index aa75950c21..5b31661507 100644 --- a/lib/util.js +++ b/lib/util.js @@ -163,9 +163,10 @@ function stylizeNoColor(str, styleType) { function arrayToHash(array) { var hash = Object.create(null); - array.forEach(function(val) { + for (var i = 0; i < array.length; i++) { + var val = array[i]; hash[val] = true; - }); + } return hash; }