Skip to content

Instantly share code, notes, and snippets.

@bluepnume
Created November 26, 2013 03:43
Show Gist options
  • Save bluepnume/7653159 to your computer and use it in GitHub Desktop.
Save bluepnume/7653159 to your computer and use it in GitHub Desktop.
'use strict';
var _ = require('underscore');
var data = {};
var timing = require('./timing');
console.log('generating');
for (var i = 1; i < 100000; i++) {
data['item' + i.toString(10)] = i;
}
function testForIn() {
var len = 0;
var keys = Object.keys(data);
var length = keys.length;
for (var i = 0; i < 100; i++) {
for (var j = 0; j < length; j++) {
len = len + data[keys[j]];
}
}
console.log(len);
}
function testEach() {
var len = 0;
function cb(k) { len = len + data[k] }
var keys = Object.keys(data)
for (var i = 0; i < 100; i++) {
_.each(keys, cb);
}
console.log(len);
}
timing.runTest('Using _.each to iterate object properties', testEach);
timing.runTest('Using for.in to iterate object properties', testForIn);
//timing.runTest('Using for.in to iterate object properties', testForIn);
//timing.runTest('Using _.each to iterate object properties', testEach);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment