Skip to content

Instantly share code, notes, and snippets.

@nodech
Created May 13, 2016 21:07
Show Gist options
  • Save nodech/dc46a10688d896c1a201b3175abd5a90 to your computer and use it in GitHub Desktop.
Save nodech/dc46a10688d896c1a201b3175abd5a90 to your computer and use it in GitHub Desktop.
Test benchmarks
var o = {};
for (var i = 0; i < 100; i++) {
o['key' + i] = 'value ' + i;
}
exports.compare = {
//#3
"for(var .. in ..)" : function () {
for(var key in o) {
test(key, o[key]);
}
},
//#4
"for(var .. in ..) OwnProperty" : function () {
for(var key in o) {
if (o.hasOwnProperty(key)) {
test(key, o[key]);
}
}
},
// #1
"Object.keys" : function () {
var keys = Object.keys(o);
for(var i = 0; i < keys.length; i++) {
test(keys[i], o[keys[i]])
}
},
// #2
"Object.keys ForEach" : function () {
var keys = Object.keys(o);
keys.forEach(function (key) {
test(key, o[key]);
})
},
};
function test(key, val) {}
require("bench").runMain()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment