object-looping
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var obj = {
a: 1,
b: 2,
c: 3,
d: 4,
f: 5
},
hasOwnProperty = Object.prototype.hasOwnProperty,
keys = Object.keys(obj);
function forInIf(obj) {
var key, value;
for (key in obj) {
if (hasOwnProperty.call(obj, key)) {
value = obj[key];
}
}
}
function forInNotHasTrue(obj) {
var key, value;
for (key in obj) {
if (!hasOwnProperty.call(obj, key)) {
continue;
} else {
value = obj[key];
}
}
}
function objectKeysForEach(obj) {
Object.keys(obj).forEach(withKey);
}
function keysForEach(keys) {
keys.forEach(withKey);
}
function forLoop(obj) {
var key, value;
for (var keys = Object.keys(obj), i = 0, j = keys.length; i < j; i++) {
key = keys[i];
value = obj[key];
}
}
function each(obj, fn) {
var key;
for (key in obj) {
if (hasOwnProperty.call(obj, key)){
fn.call(obj, obj[key])
}
}
}
function forInIfEquals(obj) {
var key, value;
for (key in obj) {
if (hasOwnProperty.call(obj, key) === true) {
value = obj[key];
}
}
}
function forInHasNotTrue(obj) {
for (key in obj) {
if (hasOwnProperty.call(obj, key) !== true) {
continue;
} else {
value = obj[key];
}
}
}
function withKey(key) {
value = obj[key];
}
function withValue(value) {
return value;
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
for in, if (true) |
|
pending… |
for in, continue (!hasOwnProperty) |
|
pending… |
Object keys |
|
pending… |
keys |
|
pending… |
for |
|
pending… |
each |
|
pending… |
for in, if (=== true) |
|
pending… |
for in, continue (!== true) |
|
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 1: published by everywhile
- Revision 2: published by Jonathan Perry
- Revision 3: published by Jean Carlo Emer
- Revision 4: published by Jonathan Perry
0 comments