Upgrade to Pro — share decks privately, control downloads, hide ads and more …

QueensJS March 4, 2015

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

QueensJS March 4, 2015

Optimizing, testing, and profiling JavaScript, especially within Node, with a focus on how V8 works.

Github: https://github.com/sandro-pasquali/v8-optimization-talk

Avatar for Sandro Pasquali

Sandro Pasquali

March 04, 2015

Other Decks in Programming

Transcript

  1. Will be talking about: - Coding patterns to avoid (sub-optimal

    codes) - Tools to help you test whether your code is optimal - Tools for profiling running code
  2. V8 is not an interpreter JS code is always running

    compiled. A first-pass (generic) compiler makes a quick pass first; next an optimizing compiler works against running code.
  3. This is Hard Dynamic typing and prototypes (among other things)

    make it difficult for a compiler to anticipate the future.
  4. Compilation Step 1 First-pass at compilation, where type analysis and

    other detailed analysis is deferred, achieving fast compilation, allowing JavaScript to “start running” almost instantly.
  5. Compilation Step 2 An optimizing compiler watches running code and

    attempts to determine future runtime characteristics. For example, if a function is executing several thousand times with the same arguments, it is optimized.
  6. Optimism is a V8 thing Optimistic, because nothing in JavaScript

    prevents a different call signature, variable type, object definition, from appearing in the future. The compiler tries to find hot functions it can make assumptions about.
  7. Good guys (sometimes) finish last Assumptions help us make decisions

    quickly, but what if we’re wrong? If V8 turns out to be wrong about the future, it must de-optimize a previously optimized function. Too many deoptimizations on a function? V8 stops trying — deoptimization hell
  8. This is the main point Predictable JavaScript can be optimized.

    Unpredictable JavaScript cannot be optimized.
  9. V8 Solves all my Problems! Compiling removes interpreter overhead. It

    can’t optimize code that…cannot be optimized (ie. whose behavior is unpredictable).
  10. Optimizing Objects V8 adds hidden classes to track properties in

    objects. - Try to not add properties after object creation - Try not to delete properties - Try to add properties in the same order. This allows V8 to share maps
  11. Not this: function Foo() { this.a = 1; this.b =

    2; } var bar = new Foo(); bar.c = 3;
  12. Advice If you use an object as if it is

    a hash table, v8 will turn it into a hash table. You don’t want that.
  13. Consistent Numbers Not good: [1, 3.4, 6, 23, 9.75] good:

    [1, 3, 6, 23, 9] ** Try not to mix integers and floats in arrays, and in assignments, and anywhere else.
  14. No sparse arrays Forces V8 to use a sub-optimal hash-table

    lookup, rather than linear storage (offsets)
  15. More about Arrays - Don’t remove elements - Start at

    zero(0) - Be consistent in array member types
  16. Avoid polymorphic functions If the arity and/or type of arguments

    in a function signature vary, that function will suffer deoptimization
  17. Inspect V8 compilation function square(operand) { return operand * operand;

    } // Make first pass to gather type information square(3); // Ask that the next call of #square trigger an optimization attempt; %OptimizeFunctionOnNextCall(square); // Call again... square(3);
  18. Remember Objects? Let’s see what happens when we delete properties

    node --allow-natives-syntax testing/objects.js
  19. What’s happening We’re going to log what v8 is doing

    while it runs a program. Then we’re going to inspect the results.
  20. You can now inspect this profile By hand (not so

    clear) By using node-tick-processor (clearer) ~ = not optimized * = optimized
  21. More info Thorsten Lorenz is doing lot of work (and

    writing) on v8 performance and profiling. https://thlorenz.github.io/v8-perf/ https://github.com/thlorenz/v8-perf/issues/4