Optimize linking of classBinding fields #1455
Conversation
|
||
for (var i = 0; i < fieldNames.length; i++) { | ||
var fieldName = fieldNames[i]; | ||
var fieldSignature = instanceSymbols[fieldName]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'm unsure if it'd make a difference for perf, but you should be able to iterate instanceSymbols property names directly:
for (var fieldName in instanceSymbols) {
var fieldSignature = instanceSymbols[fieldName];
…
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it with Object.keys because I recall something about for ... in being slow, although a simple test (http://jsperf.com/forinobjectkeys) shows that for .. in is faster.
This jsperf test is also relevant (although it doesn't measure exactly the same thing). I've seen admonitions against using for…in, for example in this StackOverflow thread and this Jon Raasch blog post. But they typically address the different use case of iterating arrays, which is not only less performant but also runs the risk of iterating non-indexed properties. The blog post also suggests that the JS engine "has to set up a function for each" iteration of a for…in loop, although I don't think that's correct. |
Optimize linking of classBinding fields
The problem with |
We aren't using for each…in, we're using for…in. So iterating over keys, not values. Does that have the same problem? |
prints:
The JS engine needs to snapshot the list of keys before iterating. In theory, this could be optimized, but @shu says that it's probably not. |
The original implementation called Object.keys() to get the keys to iterate, effectively taking a snapshot of the list of keys itself instead of relying on the JS engine to do it. Is that any better? (Either way, I think this is academic, given that the objects we're iterating are small. But I'm still curious.) |
I was just commenting on it in general, not comparing the two approaches. It seems like they would behave similarly. These days I just try to stick to C style loops. |
No description provided.