Are there any good tutorials on how to write fast, efficient code for v8 (specifically, for node.js)?
What structures should I avoid using? What are the idioms that v8 optimises well?
From my experience:
- It does inlining
- Function call overhead is minimal (inlining)
- What is expensive is to pass huge strings to functions, since those need to be copied and from my experience V8 isn't always as smart as it could be in this case
- Scope lookup is expensive (surprise)
- Don't do tricks e.g. I have a binary encoder for JS Object, cranking out some extra performance with bit shifting there (instead of Math.floor) latest Crankshaft (yes alpha, but still) runs the code 30% slower
- Don't use magic. eval, arguments.callee etc. Those pretty much kill any optimization since code can no longer be inlined
- Some of the new ES5 stuff e.g.
.bind()
is really slow in V8 at the moment
- Somehow
new Object()
and new Array()
are a bit faster currently (MICROoptimization, unless you're writing some crazy encoder stick with {}
and []
)
My rules:
- Write good code
- Write working code
- Write code that works in strict mode (support still has to land, but when it does further optimization can be applied by V8)
If you're an JS expert and your already applying all good practices to your code, there's hardly anything you can do to improve performance.
If you encounter performance issues:
- Verify them
- Change the code / algorithm
- And as a last resort: Write a C++ extension (and watch every commit to ry/node on GitHub since nobody cares whether some internal changes break your build)