Skip to content

Commit

Permalink
util: faster arrayToHash
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
JacksonTian authored and Michael Scovetta committed Apr 2, 2016
1 parent d04ae18 commit 20e2aa0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions benchmark/util/inspect.js
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 3 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 20e2aa0

Please sign in to comment.