Last active
January 21, 2016 13:23
-
-
Save akoskm/6529fa3dc2cb486b56eb to your computer and use it in GitHub Desktop.
_each vs http://draw2d.org/draw2d_touch/jsdoc_5/source/ArrayList.html#draw2d-util-ArrayList-method-each
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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