Angular foreach vs Underscore foreach vs native For Loop vs reverse native for loop
JavaScript performance comparison
Preparation code
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
(function($) {
// Create a placeholder jQuery object with a length of 1. The single item
// is completely arbitrary and will be replaced.
var jq = $([1]);
$.fn.each2 = function( fn ) {
var i = -1;
while (
// Set both the first element AND context property of the placeholder
// jQuery object to the DOM element. When i has been incremented past the
// end, this[++i] will return undefined and abort the while loop.
( jq.context = jq[0] = this[++i] )
// Invoke the callback function in the context of the DOM element,
// passing both the index and the placeholder jQuery object in. Like
// .each, if the callback returns `false`, abort the while loop.
&& fn.call( jq[0], i, jq ) !== false
) {}
// Return the initial jQuery object for chainability.
return this;
};
})(jQuery);
var sum = 0;
var arr = {};
var myFunc = function(value, key) {
sum += key + value;
}
for (var i = 0; i < 100000; i++) {
arr[i] = i*2;
}
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
angular foreach
|
|
pending… |
lodash foreach
|
|
pending… |
for loop
|
|
pending… |
for loop reverse
|
|
pending… |
for loop with caching
|
|
pending… |
browser forEach
|
|
pending… |
jQuery .each
|
|
pending… |
.each2
|
|
pending… |
native for with caching and revered
|
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
- Revision 1: published
- Revision 2: published
- Revision 3: published
- Revision 4: published Alexey
- Revision 5: published James Daily
- Revision 6: published James Daily
- Revision 7: published James Daily
- Revision 8: published
- Revision 9: published viktor
- Revision 10: published
- Revision 12: published
- Revision 13: published
- Revision 18: published
- Revision 19: published
- Revision 20: published
- Revision 21: published
- Revision 23: published test1212312
- Revision 24: published sunsean
- Revision 25: published danmasta
- Revision 26: published
- Revision 27: published Jamie
- Revision 29: published
- Revision 30: published Luscus
- Revision 31: published etc
- Revision 32: published Jimmy Jansen
- Revision 33: published Gerry
- Revision 34: published Mayank
- Revision 35: published Mike
- Revision 38: published Alejandro Rangel
- Revision 39: published belikemike
- Revision 41: published
- Revision 42: published Erik
- Revision 43: published Erik
- Revision 45: published
- Revision 46: published
- Revision 47: published
- Revision 48: published
- Revision 49: published
- Revision 50: published
- Revision 51: published
- Revision 52: published
- Revision 53: published
- Revision 54: published
- Revision 56: published
- Revision 60: published
0 Comments