each method vs. for in
JavaScript performance comparison
Info
A test of function wrappers compared to the long hand.
Preparation code
<script>
Benchmark.prototype.setup = function() {
Object.applyEach = function(target, callback) {
var key = null;
for(key in target) {
if(target.hasOwnProperty(key)) {
callback.apply(target, [callback]);
}
}
};
Object.callEach = function(target, callback) {
var key = null;
for(key in target) {
if(target.hasOwnProperty(key)) {
callback.call(target, callback);
}
}
};
var testObject = {},
copyTo = null;
for(var i = 0; i < 100; i += 1) {
testObject[i] = i * new Date().getTime();
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
applyEach |
|
pending… |
callEach |
|
pending… |
Normal loop |
|
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 Oliver Caldwell
- Revision 2: published
- Revision 3: published
- Revision 4: published by Kyle Matheny
0 comments