Skip to content

Instantly share code, notes, and snippets.

@akoskm
Last active January 21, 2016 13:23
Show Gist options
  • Save akoskm/6529fa3dc2cb486b56eb to your computer and use it in GitHub Desktop.
Save akoskm/6529fa3dc2cb486b56eb to your computer and use it in GitHub Desktop.
var Array = {
create: function(arr) {
if (arr) {
this.data = arr;
} else {
this.data = [];
}
return this;
},
each: function(func, reverse) {
if(typeof reverse !=="undefined" && reverse===true){
for (var i=this.data.length-1; i>=0; i--) {
if(func(i, this.data[i])===false)
break;
}
} else {
for (var i=0; i<this.data.length; i++) {
if(func(i, this.data[i])===false)
break;
}
}
}
};
var arr = Array.create([{
val: 2
}, {
val: 4
}, {
val: 8
}]);
var val = 0;
console.time('custom each');
arr.each(function(e) {
val = val + e.val;
});
console.timeEnd('custom each');
var _val = 0;
console.time('_each');
_.each(arr.data, function(e) {
_val = _val + e.val;
});
console.timeEnd('_each');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment