Skip to content

Instantly share code, notes, and snippets.

@cerivera
Last active April 6, 2017 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cerivera/8534081 to your computer and use it in GitHub Desktop.
Save cerivera/8534081 to your computer and use it in GitHub Desktop.
Underscore.js foreach vs native for loop speed test
// Build 100000 alphabet dictionaries
var dict = {};
for (var i = 0; i < 100000; i++) {
dict[i] = {};
for (var c = 65; c < 65+26; c++) {
dict[i][String.fromCharCode(c)] = c;
}
}
// Loop through parent and child dictionaries in native JS
var t1 = new Date().getTime();
var length = Object.keys(dict).length;
for (var i = 0; i < length; i++) {
var subLength = Object.keys(dict[i]).length;
for (var l = 0; l < subLength; l++) {
}
}
var t2 = new Date().getTime();
console.log("For loop: " + (t2 - t1)/1000 + "s");
// Underscore
var t3 = new Date().getTime();
_.each(dict, function(v, k, l) {
_.each(v, function(v2, k2, l2) {
});
});
var t4 = new Date().getTime();
console.log("Underscore.js foreach: " + (t4 - t3)/1000 + "s");
@Vnthf
Copy link

Vnthf commented Oct 6, 2016

What is conclusion?

@lukecampbell
Copy link

2017-04-05 Chrome Version 56.0.2924.87 (64-bit):

For Loop: 0.143s
Underscore.js foreach: 0.171s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment