Squeezing performance out of v8

前端 未结 3 1698
不知归路
不知归路 2021-02-04 09:12

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 o

相关标签:
3条回答
  • 2021-02-04 09:37

    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)
    0 讨论(0)
  • 2021-02-04 09:52

    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.

    0 讨论(0)
  • 2021-02-04 10:04

    The docs give a great answer: http://code.google.com/apis/v8/design.html

    0 讨论(0)
提交回复
热议问题