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

Dissecting Node.js – Understanding the most famous JS runtime to the bare bone

Dissecting Node.js – Understanding the most famous JS runtime to the bare bone

Lucas Santos

April 04, 2020
Tweet

More Decks by Lucas Santos

Other Decks in Technology

Transcript

  1. agenda_ - What is JavaScript - What is Node.js -

    Our little function - How JavaScript works - V8 and its sorceries - V8 compiling pipelines - Putting it all together
  2. JavaScript The language everyone loves to hate - Prototype-based -

    Dynamically-typed - Interpreted language - Created in 1995 as a scripting language for Netscape - Created by Brendan Eich - Conforms with ECMA262 (ECMAScript) - Defined/Maintained by TC39
  3. Node.js The angel that brought JS to the world again

    - JavaScript server-side runtime - Created in 2009 by Ryan Dahl - Later sponsored by Joyent - Event loop - Non blocking I/O - Uses V8
  4. What is an engine? - Runs all JS code we

    write - Responsible for - parsing and interpreting - memory allocation - the call stack - garbage collection - code optimizations
  5. There are many - V8 -> made by Google for

    Chrome and some Chromium-based browsers - Chakra -> made by Microsoft for Microsoft Edge - SpiderMonkey -> made by Netscape, now runs on Firefox - Rhino -> made by Mozilla, in Java - Nitro -> made by Apple for Safari - JerryScript, Espruino -> IoT - And many others...
  6. what means to be single-threaded? - We only have one

    call stack - We can only run one thing at a time
  7. call stack - Not part of JS - Provided by

    V8 - Data structure - Ordered as LIFO - Keeps track of function calls - Made of stack frames
  8. hosting environment - Wherever place the code is running on

    - Provides external APIs - Handles async queues
  9. event loop - Handle async code - Puts queued callbacks

    onto the call stack - In Node, provided by libuv - Not part of V8 - Not part of JS
  10. libuv more than an event loop - Open source -

    Created for node - Wrapper around libev - Provides - Event loop - TCP & UDP - DNS Resolution - FS Events - IPC and Child processes - Thread pool - More...
  11. Call Stack Ext. APIs CB queue Starting Ending The file

    contents cb(err, data) console.log(data.toS())
  12. Call Stack Ext. APIs CB queue Job queue timer cb3

    handle setTimeout('t3',0) cb1 cb2
  13. Call Stack Ext. APIs CB queue Job queue handle fn

    setTimeout('tp',0) cb1 cb2 cb3
  14. Call Stack Ext. APIs CB queue Job queue timer cb4

    handle fn setTimeout('tp',0) cb1 cb2 cb3
  15. Call Stack Ext. APIs CB queue Job queue cb2 cb3

    cb4 cb1 console.log('timeout')
  16. Call Stack Ext. APIs CB queue Job queue cb2 cb3

    cb4 cb1 console.log('timeout') start promise timeout
  17. Call Stack Ext. APIs CB queue Job queue cb3 cb4

    cb2 console.log('timeout2') start promise timeout timeout2
  18. Call Stack Ext. APIs CB queue Job queue cb4 cb3

    console.log('timeout3') start promise timeout timeout2 timeout3
  19. Call Stack Ext. APIs CB queue Job queue cb4 console.log('p

    timeout') start promise timeout timeout2 timeout3 promise timeout
  20. V8

  21. V8 the golden compiler - Made by Google - open

    source high-performance - JS and WebAsm engine - Written in C++ - Implements ES fully - Can be standalone
  22. C2

  23. FCG compiler fast, but not that good - First step

    of the pipeline - Simple and fast - Not-optimised, relatively slow - Handles type-feedback, finds hot functions - Takes the AST and generate generic native code
  24. Crankshaft compiler good, but not that fast - Takes type-feedback

    from FCG - Optimises code accordingly - Replaces not-opt code with optimised code using OSR - Does not implement all ES - Made of two components - Hydrogen - Lithium
  25. TurboFan Crankshaft, the right way - Started as a secondary

    compiler for ES6 - Designed to be a webasm compiler - Uses a sea of nodes - Receives bytecodes directly - Divided in layers - Frontend - Optimising Layer - Backend - Fixed deoptimisation cliffs
  26. Ignition an interpreter to rule them all - JS Interpreter

    - Generates optimised bytecode - Created to reduce memory usage - Focused on low memory devices (cell phones) - NOT a parser - AST not source of truth anymore - Uses Turbofan's backend
  27. references_ - https://dev.to/khaosdoctor/node-js-under-the-hood-1-getting-to-know-our-tools-1465 - https://dev.to/khaosdoctor/node-js-under-the-hood-2-understanding-javascript-48cn - https://dev.to/khaosdoctor/node-js-under-the-hood-3-deep-dive-into-the-event-loop-135d - https://dev.to/khaosdoctor/node-js-under-the-hood-4-let-s-talk-about-v8-1eol -

    https://dev.to/khaosdoctor/node-js-under-the-hood-5-hidden-classes-variable-allocations-1244 - https://dev.to/khaosdoctor/node-js-under-the-hood-6-the-old-v8-34hm - https://dev.to/khaosdoctor/node-js-under-the-hood-7-the-new-v8-4gd6 - https://dev.to/khaosdoctor/node-js-under-the-hood-8-oh-the-bytecodes-1p6p - https://dev.to/khaosdoctor/node-js-under-the-hood-9-collecting-the-garbage-772