Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: faster arrayToHash #3964

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -161,9 +161,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++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that avoiding accessing the length property each time will speed up it a little more.

var l = array.length;
var val;
var i;

for (i = 0;  i < l; i++) {
  val = array[i];
  hash[val] = true;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V8 can speedup it with OSR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make it do more work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using let here might have a more clear scope definition?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v8 is smart enough these days to cache the length automatically, so this shouldn't be an issue.

var val = array[i];
hash[val] = true;
});
}

return hash;
}
Expand Down