An excellent overview! A bit worrying that using an object as a hash table and iterating over its keys using ForIn would prevent optimization - I had always thought that this was a common use case that would be well-supported by the optimizer! I suppose in that case, if you need fast reads of all keys and can afford slower writes, you could maintain an array of the keys at insertion time and just loop through that?
`for ... in` is a relatively slow construct anyway, the faster option (a lot more verbose) is: var keys = Object.keys(obj), length = keys.length, key, i; for (i = 0; i While this is not exactly the same as for..in, it usually behaves how you'd expect and is significantly faster for a couple of reasons: 1. In a for..in loop the engine must keep track of the keys already iterated over, whereas in the fast version we ca…
V8 Optimization Killers
41–50 of 88 posts
Re: V8 Optimization Killers
#42Earlier quoted context omitted.
I think minifiers use the former because it's shorter, and thus requires less space.
the other benefit is that `void 0` cannot be overwritten in sloppy mode, whereas you can do something crazy like this: undefined = 'lol';
Re: V8 Optimization Killers
#43Really great! Some notes that popped out for me are that to always cache the .length property for any array or arguments: function doesntLeakArguments() { var args = new Array(arguments.length); for(var i = 0; i becomes: function doesntLeakArguments() { var len = arguments.length; var args = new Array(len); for(var i = 0; i And also, if you've got a switch statement with more than 128 cases, you've probably got bigge…
This used to be good advice, but I'm pretty sure that the modern engines all perform this optimization for you now.
It looks like you're right, very right unless there's something else going on in the perf tests.
Re: V8 Optimization Killers
#44Earlier quoted context omitted.
the other benefit is that `void 0` cannot be overwritten in sloppy mode, whereas you can do something crazy like this: undefined = 'lol';
No you can't. You can do something like function(undefined) {}, but the global 'undefined' is non-writable and non-configurable.
Re: V8 Optimization Killers
#45An excellent overview! A bit worrying that using an object as a hash table and iterating over its keys using ForIn would prevent optimization - I had always thought that this was a common use case that would be well-supported by the optimizer! I suppose in that case, if you need fast reads of all keys and can afford slower writes, you could maintain an array of the keys at insertion time and just loop through that?
`for ... in` is a relatively slow construct anyway, the faster option (a lot more verbose) is: var keys = Object.keys(obj), length = keys.length, key, i; for (i = 0; i While this is not exactly the same as for..in, it usually behaves how you'd expect and is significantly faster for a couple of reasons: 1. In a for..in loop the engine must keep track of the keys already iterated over, whereas in the fast version we ca…
Re: V8 Optimization Killers
#46Earlier quoted context omitted.
This used to be good advice, but I'm pretty sure that the modern engines all perform this optimization for you now.
Try this benchmark out: http://jsperf.com/fastest-array-loops-in-javascript/56 It looks like you're right, very right unless there's something else going on in the perf tests.
Re: V8 Optimization Killers
#47I created a jsperf to measure a hashtable-like object with for-in/Objekt.keys The performance differs only with 50%, I expected more actually. Am I doing something wrong? http://jsperf.com/for-in-with-hashtable-like-object
Re: V8 Optimization Killers
#48Re: V8 Optimization Killers
#49Earlier quoted context omitted.
I'm really sick of these recent JavaScript performance related posts that only talk about V8.
Just a cursory glance at browser stats seems to show chrome as the most popular which would make sense. I highly doubt anyone using internet explorer is coming to SO.
Also nice that IE doesn't still require prefixes for established things like transition/transform. If only Chrome and others would get around to implementing other useful CSS3 features that IE has (like grid) we might be able to actually use them some day.