Object.keys-vs-for-in-hasOwnProperty Anonymous Object
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var anObject = {
a:"A",
b:"B",
c:"C",
d:"D",
e:"E",
f:"F",
g:"G",
h:"H",
i:"I",
j:"J",
k:"K",
l:"L",
m:"M",
n:"N",
o:"O",
p:"P",
q:"Q",
r:"R",
s:"S",
t:"T",
u:"U",
v:"V",
w:"W",
x:"X",
y:"Y",
z:"Z"
};
var anObject2 = Object.create(anObject,{
aa: {value: "aa"},
bb: {value: "bb"},
cc: {value: "cc"},
dd: {value: "dd"},
ee: {value: "ee"},
ff: {value: "ff"},
gg: {value: "gg", enumerable: true},
hh: {value: "hh", enumerable: true},
ii: {value: "ii", enumerable: true},
jj: {value: "jj", enumerable: true},
kk: {value: "kk", enumerable: true},
ll: {value: "ll", enumerable: true}
})
function loopObjectForInHasOwnProperty(anObject) {
for(var p in anObject) {
if(anObject.hasOwnProperty(p)) {
}
}
}
function loopObjectForObjectKeys(anObject) {
var keys = Object.keys(anObject);
for(var i=0, countI=keys.length;i<countI;i++) {
}
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
loopObjectForInHasOwnProperty anObject |
|
pending… |
loopObjectForObjectKeys anObject |
|
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
- Revision 2: published by Just another JS hacker
- Revision 3: published by Krinkle
- Revision 4: published
- Revision 6: published
- Revision 7: published
0 comments