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?
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?
Share Improve this question asked Jan 17, 2011 at 5:58 nornagonnornagon 15.8k18 gold badges76 silver badges86 bronze badges 3- Just curious, but why are you asking? Shouldn't you be more concerned about squeezing performance out of the slowest (but widely used) rendering engines than the fastest one? Seems the fastest will take care of itself. – mVChr Commented Jan 17, 2011 at 6:29
- I just want to know what v8 doesn't like. Efficient code matters to me. – nornagon Commented Jan 17, 2011 at 6:54
- Good reply nornagon - its incredibly frustrating when the question is disregard and responses are unrelated. I too am interested simply for interests sake! – Chris GW Green Commented Apr 4, 2014 at 9:17
3 Answers
Reset to default 18From 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()
andnew 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)
The docs give a great answer: http://code.google.com/apis/v8/design.html
Understanding V8 is a set of slides from nodecamp.eu and gives very some interesting tips. In particular, I found the notes on avoiding "dictionary mode" useful i.e. it helps if you keep the "shape" of objects constant and don't add arbitrary properties to them.
You should also run node with --crankshaft --trace-opt --trace-bailout
(the --crankshaft
is only needed on 64-bit platforms e.g. OS X) to see whether V8 is "bailing" on JITing certain functions. There is a ton of other trace options including --trace-gc
and various other GC tracing, which can be useful for optimisation.
Let me know if you have any specific questions about the slides above as they're a bit concise. :-) They're not mine but I've done some research about the areas they cover.