for vs forEach
JavaScript performance comparison
Info
Is it faster to use the native forEach or just loop with for?
Inspired by Adrian Sutton's tests at: http://www.symphonious.net/2010/10/09/javascript-performance-for-vs-foreach/
This one adds random floating point numbers to see if the loop overhead is significant at all in the face of standard work.
Preparation code
<script>
Benchmark.prototype.setup = function() {
var i,
value,
length,
values = [],
sum = 0,
context = values;
for (i = 0; i < 10000; i++) {
values[i] = Math.random();
}
function add(val) {
sum += val;
}
function addEach(k, val) {
sum += val;
}
var toString = Object.prototype.toString;
function isArray(obj) {
return toString.call(obj) === '[object Array]';
}
function isObject(obj) {
return toString.call(obj) === '[object Object]';
}
function isString(obj) {
return toString.call(obj) === '[object String]';
}
function each(obj, iterator) {
var key, length;
if (!obj) {
return;
}
length = obj.length;
if (isArray(obj) || isString(obj)) {
for(key = 0; key < length; key += 1) {
iterator(obj[key], key, obj);
}
return obj;
}
if (isObject(obj)) {
for(key in obj) {
if (obj.hasOwnProperty(key)) {
iterator(obj[key], key, obj);
}
}
return obj;
}
return obj;
};
function each1 (data, iterator) {
var i,
length = data.length;
for (i = 0; i < length; i += 1) {
iterator(data[i], i, data);
}
}
function each2 (data, iterator) {
var i,
length = data.length;
for (i = 0; i < length; ++i) {
iterator(data[i], i, data);
}
}
};
Benchmark.prototype.teardown = function() {
i = 0;
value = 0;
length = 0;
values = [];
sum = 0;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
strict 0 comparison |
|
pending… |
for loop, cached length, callback |
|
pending… |
for loop, cached length, callback, reduce op |
|
pending… |
new each |
|
pending… |
without strict zero comparison |
|
pending… |
each1 |
|
pending… |
each2 |
|
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. Here’s a list of current revisions for this page:
- Revision 2: published by Adrian Sutton
- Revision 3: published by Adrian Sutton
- Revision 5: published by Adrian Sutton
- Revision 9: published by alk
- Revision 10: published
- Revision 12: published
- Revision 15: published
- Revision 16: published by RubaXa
- Revision 17: published
- Revision 20: published by Brandon Satrom
- Revision 24: published
- Revision 26: published by cfddream
- Revision 27: published by cfddream
- Revision 31: published by spongman
- Revision 32: published by James Pamplin
- Revision 36: published
- Revision 37: published
- Revision 38: published by Sujay
- Revision 46: published by wicked
- Revision 47: published
- Revision 48: published by smith
- Revision 49: published by smith
- Revision 52: published
- Revision 53: published by Jean Vincent
- Revision 54: published
- Revision 56: published
- Revision 57: published by Gajus
- Revision 58: published
- Revision 59: published
- Revision 60: published
- Revision 61: published and last updated
- Revision 62: published
- Revision 63: published
- Revision 64: published
- Revision 65: published
- Revision 66: published by gerhard
- Revision 67: published
- Revision 68: published
- Revision 69: published
- Revision 70: published
- Revision 71: published
- Revision 75: published
- Revision 76: published
- Revision 77: published
0 comments