244 points · by diggan · 12y ago · 80 comments
Please don't memorize this list. It's just for one JS engine among several, all of which are changing rapidly. For example Apple just shipped FTL, an LLVM-based JIT for Safari (so any list for Safari would have just been largely obsoleted).
If you are writing code for nodejs, none of the other engines are relevant. If the overwhelming majority of your users are using chrome, then v8-specific optimizations aren't a bad idea.
On the issue of the list, most of the guidelines here apply here and in every other engine. For example, I doubt a future JS engine can optimize `arguments` abuse while preserving ES5 semantics.
Currently not optimizable:
Generator functions
Functions that contain a for-of statement
Functions that contain a try-catch statement
Functions that contain a try-finally statement
Functions that contain a compound let assignment
Functions that contain a compound const assignment
Functions that contain object literals that contain __proto__, or get or set declarations.
Likely never optimizable:
Functions that contain a debugger statement
Functions that call literally eval()
Functions that contain a with statement
Which I now realize is sort of what I'm looking for.SpiderMonkey now optimizes a bunch of difficult cases it never optimized before; some of them seemed like they would never get optimized, but now they're fine. V8 does some nice optimizations now that weren't possible before, similarly.
var x = 5; something.propertyOnSomething = 5 with(something) { x += propertyOnSomething } alert(x); // What is the value of x?
eval() causes problems as it can introduce new variables into the local scope making it logically difficult to reason about program behaviour subsequent to the eval(), e.g.
function f(x) { eval(something); return Math.sqrt(x); }
What is the result of f(9)?
It is possible to set things up to make these not catastrophically harm performance, but the question is whether you would rather the engineers working on these engines make uncommon (and arguably bad) things common, or make normal and frequent code fast. Any time spent working on making uncommon things fast is time not spent making other things fast.
function doesntLeakArguments() {
var args = new Array(arguments.length);
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
return args;
}
becomes: function doesntLeakArguments() {
var len = arguments.length;
var args = new Array(len);
for(var i = 0; i < len; ++i) {
args[i] = arguments[i];
}
return args;
}
And also, if you've got a switch statement with more than 128 cases, you've probably got bigger problems on your hands than v8 optimizations.I see some things in here that I can start switching to immediately for some of my node modules.
In the Javascript world, "Your Mileage May Vary" truly is a motto.
Using a hashtable for instruction dispatch in an emulator would be utterly insane. You'd use an array (but the compiler can do a better job by generating raw asm for a jump table.)
It looks like you're right, very right unless there's something else going on in the perf tests.
for(var i=0;i<a.length;i++) if(check(a[i])) a.push('foo');
Is it probable that SpiderMonkey and other engines suffer from forms of this deoptimization hell when rendering? And would it be a safe guess that whatever mod_pagespeed does to a page's javascript resources is less likely to result in this hell than these other tools? Thanks.
I'm not sure about SpiderMonkey, but they may very well use this kind of optimization too. However, I would guess that Uglifying files should not increase execution speed for any noticeable amount.
here says: http://www.2ality.com/2013/04/check-undefined.html void 0 is safe.
http://jsperf.com/check-for-undefined-argument
On Chrome 35, looks like comparing to void 0 is a bit faster. Would love some more samples though.
var keys = Object.keys(obj),
length = keys.length,
key, i;
for (i = 0; i < length; i++) {
key = keys[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 can simply increment a counter and do a fast array lookup.
2. It's possible to add properties to the object that you're iterating within the body of the for..in statement, and these will be iterated too. Doing such a thing is obviously very rare but it's the kind of edge case the JS engine must keep track of.
Object.keys(obj).forEach(function (key) {
console.log(obj[key]);
});
but does that hamstring my performance?But note that in your specific case chances are the cost of a bunch of console.log() calls completely swamps the cost of either a for loop or a forEach call. console.log() has _incredibly_ complicated behavior.
Deleting properties is guaranteed to not break iteration and deleting an as-of-yet unvisited property causes it to never be visited.
The performance differs only with 50%, I expected more actually. Am I doing something wrong?
Maybe apart from games, most of this is irrelevant for browser side javascript.
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.
The "not something a human should ever generate" doesn't seem like a right statement : before a transpiler can generate this kind of code, a human has to understand it.
It's like complaining if a C++ performance article only tested with GCC. Sure, there's other compilers out there, but that's the big one.
- manipulating `arguments` should be avoided
- when possible, avoid looping over the properties of an object (using `in` or `Object.keys`)
In the process of optimizing for v8, you may expose other bad patterns (e.g. megamorphic functions) whose replacement will boost performance everywhere.
For example, even though https://github.com/petkaantonov/deque was developed and tuned for v8, it is still much faster than alternatives in other engines.
http://jsfiddle.net/G83mW/14/ has a testcase for the try/catch thing that is interesting to compare in different browsers...