Why is the fourth nested value way slower to access than the fifth?
javascript, performance
Solution
Your object is this, my comment added:
var obj = {
"one": {
"two": {
"three": {
"four": {
"five": {
"value": 0
}
/* MISSING "value": 0 */
},
"value": 0
},
"value": 0
},
"value": 0
},
"value": 0
};
};
The object at the `"four"` key doesn't have a `"value"` key, however, so apparently the JavaScript engine(s) have to do extra work to handle that case: miss the key look-up on the object, miss the key look-up on the object's prototype `Object`, return `undefined`, and then compute `NaN` when you add `1` to `undefined`.
Problem
I was building a jsperf to illustrate time it took to access nested object members and I found this strange phenomenon. For some reason, the test run drastically slower for the fourth-nested object member than the fifth. I've tried this in Chrome and Firefox and am getting the same results. Any ideas why this would be happening?