AngularJS forEach vs Native forEach vs key vs for
JavaScript performance comparison
Preparation code
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js">
</script>
<script>
var len = 1000;
var arr = new Array();
while (len--) {
arr[len] = len;
}
</script>
<script>
Benchmark.prototype.setup = function() {
var a = angular, u = {};
a.forEach = function(obj, iterator, context) {
var key, objLen;
if (obj) {
if (a.isFunction(obj)) {
for (key in obj) {
// Need to check if hasOwnProperty exists,
// as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
if (key !== 'prototype' && key !== 'length' && key !== 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
iterator.call(context, obj[key], key);
}
}
} else if (u.isArray(obj) || u.isArrayLike(obj)) {
objLen = obj.length; key = 0;//cached length is faster!
for (; key < objLen; key++) {
iterator.call(context, obj[key], key);
}
} else if (obj.forEach && obj.forEach !== a.forEach) { //https://github.com/angular/angular.js/commit/36625de0d3ebc1fc091af474d942c6ce16b0a1c0
obj.forEach(iterator, context);
} else {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key);
}
}
}
}
return obj;
};
u.isWindow = function(obj) {
return obj && obj.document && obj.location && obj.alert && obj.setInterval;
};
u.isArray = (function() {
if (!a.isFunction(Array.isArray)) {
return function(value) {
return toString.call(value) === '[object Array]';
};
}
return Array.isArray;
})();
u.isArrayLike = function(obj) {
if (obj === null || u.isWindow(obj)) {
return false;
}
var length = obj.length;
if (obj.nodeType === 1 && length) {
return true;
}
return a.isString(obj) || a.isArray(obj) || length === 0 || typeof length === 'number' && length > 0 && (length - 1) in obj;
};
function test7WithFn(arr, fn) {
var i = 0, arrLen = arr.length;
for (; i < arrLen; i++) {
fn(arr[i]);
}
}
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
Native forEach
|
|
pending… |
AngularJS forEach
|
|
pending… |
for key in arr
|
|
pending… |
for loop
|
|
pending… |
for loop, super-cached i and arr length, simpler!
|
|
pending… |
for loop, simple (with var)
|
|
pending… |
for loop, improved super-cached i and arr length, even simpler!
|
|
pending… |
AngularJS forEach with Improved forEach Method
|
|
pending… |
|
|
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
- Revision 5: published
- Revision 6: published
- Revision 7: published
- Revision 8: published
- Revision 9: published
- Revision 10: published
- Revision 12: published Dylan
- Revision 13: published
- Revision 14: published Jim Marion
- Revision 15: published Tom Taylor
- Revision 16: published
- Revision 17: published Tom Taylor
- Revision 18: published
- Revision 19: published
- Revision 20: published Tom Taylor
- Revision 21: published IgorMinar
- Revision 22: published John
- Revision 23: published
- Revision 24: published
- Revision 25: published Gerard
- Revision 26: published
- Revision 27: published Marc
- Revision 28: published
- Revision 29: published
- Revision 31: published
- Revision 34: published mr
- Revision 35: published Tom Taylor
- Revision 36: published Carlos
- Revision 39: published
- Revision 41: published
- Revision 42: published
- Revision 43: published
- Revision 44: published
- Revision 45: published David V
- Revision 46: published
- Revision 49: published Ishan Misra
- Revision 50: published
0 Comments