Why does NodeJS upgrade speed up? Why is NodeJS code so slow to start and faster to run? What is a V8 JIT?

preface

V8 product definition: Speed up real-world performance for modern JavaScript, and enable developers to build a faster Future Web.

Recently, I have watched three videos of V8 core group, which I feel are very valuable. I will be a porter (students with good English can watch them by themselves). I plan to divide the three videos into three articles:

  • The first source, the V8 team’s product manager; V8, Advanced JavaScript, & the Next Performance Frontier (Google I/O ’17)
  • The second source, V8 syntax parsing; Parsing JavaScript – better lazy than eager?
  • The third source, compilation and running of V8; How JavaScript Engines Work

After the language

First out the conclusions:

V8 execution pipeline architecture diagram

  1. V8 Syntax parsing phase:

    • JS syntax parsing speed: about 1MB/s; 400K JS code, syntax parsing needs ~370ms
    • Check the parsing time of your code

      `(function eager(){... }) (); `Copy the code
    • Run as little code as possible 🙂
  2. V8 Build phase:

    • Try to write “statically typed” code
    • WebAssembly uses TurboFan
  3. V8 JS code:

    • Async/Await (4.5x) is almost twice faster than Generators(2.5x)
    • The SPEED of ES2015 is getting closer to that of ES5
  4. V8 for NodeJS:

    • Node –inspect app.js && Chrome ://inspect/#devices
  5. V8 for browser:

    • Coverage feature, which checks Coverage of running code

Why V8? -V8 product manager’s description

For our first post, let’s take a look at what the V8 product manager thinks of the V8 engine. Overview of V8’s code execution pipeline architecture.

V8 metrics

As a JS engine, we need to consider not only performance increase, but also startup speed and memory usage. The V8 product managers present their model for measuring the V8 engine and V8 usage scenarios.

  • Scene 1:
foo(42);Copy the code

Execute only one function; Expect parsing to start quickly and compile to run;

  • Scene 2:
for (var i=0; i<10000; i++) {
    foo(i);
}Copy the code

Execute foo 10,000 times; In a PC browser /NodeJS server, the foo function is expected to run high;

Execute foo 10,000 times; On mobile browsers /NodeJS IoT devices, expect low memory and high performance to run function Foo;

V8 execution pipeline

How on earth does V8 switch between fast start/peak performance, low memory/high optimized memory gears?

TurboFan(TurboFan)

V8 optimized compiler, accumulated over 3 years

  • Used to improve code performance
  • Support and optimize Es2015+ features
  • The background of WebAssembly

Ignition(Ignition)

Small memory footprint, fast startup

  • Originally designed for low-memory devices, memory saving is now available on all platforms;
  • Fast generation of binary code, improve the speed of page startup;
  • Fast start and performance optimization is made easier with TurboFan;

Ignition+TurboFan

New JavaScript Pipeline for 2017

  • JS code is faster
  • Less memory
  • New performance improvements
  • More suitable for NodeJS
  • No longer defaulting on new JS features
  • Rewrite built-in functions (up to 4x faster)

Orinoco

Almost parallel and concurrent compression algorithm GC

  • Parallel compression, parallel pointer update, avoid the old and new generation of frequent updates to improve page speed, concurrent exchange
  • Support now: concurrent markup

Speedometer2

V8 was used to test JS benchmarks, which is more appropriate

  • Test TodoList, the latest version of the popular MVC framework
  • Almost modest is the best browser benchmarks

Optimize ES2015

  • Generators increase speed by 2.5 times
  • Async/Await speed increased 4.5 times
  • Promise 4 times faster

V8 love NodeJS

V8 is a core member of the NodeJS committee and continues to improve NodeJS performance

AcmeAir benchmark

The benchmark NodeJS

Node + DevTools

Easier to debug NodeJS code

  • At the start and--inspectparameternode --inspect index.js
  • Debug the Node code using the About: Inspect link in the Chrome address bar
  • Support for asynchronous debugging, profiling and more

Code Coverage function

See what code is not being executed

WebAssembly

Secure cross-browser native code

  • Chrome and FireFox support, including Edge and Safari in the future
  • Compile C/C++ to the Web with Emscripten Toolchain
  • Future plans: better performance; Easier to instantiate from a Response Object; Support for sharing ArrayBuffers(Chrome 60)

summary

The execution performance of JavaScript depends on the environment and context.

Ignition+TurboFan+Webassambly, which enables V8 at fast start/peak performance; Low memory/high optimized memory; Any switch before each gear;