Thursday, 26 September 2013

Chrome performance: "legal" property names vs

Chrome performance: "legal" property names vs

So this is an interesting one... While I was testing the performance of
setAttribute vs. normal property set on an element, I found an odd
behavior, which I then tested on regular objects and... It's still odd!
So if you have an object A = {}, and you set its property like
A['abc_def'] = 1, or A.abc_def = 1, they are basically the same. But then
if you do A['abc-def'] = 1 or A['123-def'] = 1 then you are in trouble. It
goes wayyy slower. I set up a test here: http://jsfiddle.net/naPYL/1/.
They all work the same on all browsers except chrome. The funny thing is
that for "abc_def" property, chrome is actually much faster than Firefox
and IE, as I expected. But for "abc-def" it's at least twice as slow.
So what happens here basically (at least from my tests) is that when using
"correct" syntax for properties (legal C syntax, which you can use with
dot properties) - It's fast, but when you use syntax that requires using
brackets (a[...]) then you're in trouble.
I tried to imagine what implementation detail would distinguish in such a
way between the two modes, and couldn't. Because as I think of it, if you
do support those non-standard names, you are probably translating all
names to the same mechanics, and the rest is just syntax which is compiled
into that mechanic. So . syntax and [] should be all the same after
compilation. But obviously something is going the other way around here...
Without looking at V8's source code, could anyone think of a really
satisfying answer? (Think of it as an exercise :-))

No comments:

Post a Comment