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

WebAssembly in NodeJS

WebAssembly in NodeJS

My presentation about WASM in NodeJS @ NodeJS Meetup Berlin

Willian Martins

October 17, 2017

More Decks by Willian Martins

Other Decks in Technology


  1. How did we achieve it Moore law Pushed Tech industries

    to evolve until today Allowed huge computation power even on small devices
  2. All this power for what? Faster applications More immersive games

    Virtual reality Augmented reality Live video/image processing Big Data/Machine learning
  3. JS - Early days Created in 1995 by Brendan Eich

    Was written in 11 days Designed to be easy and simple Designed to be a glue code for dynamic Web
  4. JS - 2008 until now 2008 - JIT Compiler by

    google w/ Chrome Jump of performance Allowed more complex applications What is the next step?
  5. What’s WebAssembly? New binary format Run compiled programs (C, C++,

    Rust) on a browser Works alongside Javascript Performance and flexibility API
  6. JiT: Code life cycle in summary 1. Parse 2. Compile

    3. Optimize (de-optimize) 4. Execute 5. Garbage Collector
  7. WebAssembly is fast WASM is more compact -> Faster FETCH

    of the source WASM is closer to machine code -> Faster DECODING, COMPILING and OPTIMISING No need to RE-OPTIMISE No garbage collection
  8. How to run WASM modules Current situation: not possible to

    run WASM modules on their own Need for some Javascript glue
  9. fs.readFile(‘./module.wasm’, async function(err, file) => { const { instance }

    = await WebAssembly.instantiate(file); // Do something with the compiled results! });
  10. Export functions to JS Keyword EMSCRIPTEN_KEEPALIVE EMSCRIPTEN_KEEPALIVE int sum(int x,

    int y) { return x + y; } Expose only the interface of the WASM module to JS
  11. Memory management Emscripten provide three useful functions to manage WebAssembly

    memory _malloc(memoryNeeded) getValue(ptr, type) setValue(ptr, value, type)
  12. “Currently, calling a WebAssembly function in JS code is slower

    than it needs to be. The JIT doesn’t know how to deal directly with WebAssembly, so it has to route the WebAssembly to something that does. … This can be up to 100x slower than it would be if the JIT knew how to handle it directly. ” Lin Clark
  13. Future features Formal Specification
 Threads supports
 SIMD acronym to Single

    Instruction Multiple Data (back to Stage 3 on TC39) 
 Exception handling
 Garbage collection
 Direct DOM access
 ES6 Module integration