Skip to content

Commit 20e2aa0

Browse files
JacksonTianMichael Scovetta
authored and
Michael Scovetta
committed
util: faster arrayToHash
The `util.format()` is used frequently, make the method faster is better. R-URL: nodejs#3964 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d04ae18 commit 20e2aa0

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ bench-url: all
493493
bench-events: all
494494
@$(NODE) benchmark/common.js events
495495

496+
bench-util: all
497+
@$(NODE) benchmark/common.js util
498+
496499
bench-all: bench bench-misc bench-array bench-buffer bench-url bench-events
497500

498501
bench: bench-net bench-http bench-fs bench-tls

benchmark/util/inspect.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var util = require('util');
2+
3+
var common = require('../common.js');
4+
5+
var bench = common.createBenchmark(main, {n: [5e6]});
6+
7+
function main(conf) {
8+
var n = conf.n | 0;
9+
10+
bench.start();
11+
for (var i = 0; i < n; i += 1) {
12+
var r = util.inspect({a: 'a', b: 'b', c: 'c', d: 'd'});
13+
}
14+
bench.end(n);
15+
}

lib/util.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ function stylizeNoColor(str, styleType) {
163163
function arrayToHash(array) {
164164
var hash = Object.create(null);
165165

166-
array.forEach(function(val) {
166+
for (var i = 0; i < array.length; i++) {
167+
var val = array[i];
167168
hash[val] = true;
168-
});
169+
}
169170

170171
return hash;
171172
}

0 commit comments

Comments
 (0)