From back-end to front-end Киев 2018 WebAssembly vs JavaScript Скачков Александр @alSkachkov
ABOUT ME • Itera’s FrontEnd Tech Lead • WebKit contributor: • Arrow function • Async iterator & generators • Promise.finally • Twitter: @alSkachkov WebAssembly vs JavaScript Киев 2018
• What was before • WebAssembly vs JavaScript • WebAssembly – in real life
JavaScript one of the most popular language
THE STRENGTH IS A WEAKNESS • Easy to learn • King of the web • Dynamic typing = • NOT PREDICTIBLE PERFORMANCE!
TRIES TO SOLVE asm.js
SMART PEOPLE SAYS Always bet on JS & WASM
Always bet on JS WASM?
• Announced in 17 June 2015 • Supported by major Browsers • Developed by WebAssembly Working Group (W3C) • Draft specific published 15 Feb 2018 WebAssembly – New Hope
What is WebAssembly (aka WASM)?
WebAssembly is build target
safe, portable, low-level code format designed for efficient execution and compact representation https://webassembly.github.io/spec/intro
C - Source code
RUST - Source code
S - Expression
fast to load Byte code
Will WebAssembly replace JavaScript? Not now 
but WebAssembly will transform web
WASM WILL BRING TO WEB • High performance • New languages • Variety of existed libraries
WHAT'S NEW IN THE WEB • New types of applications will appear in Web • graphical, video and audio editors • games that fully consume CPU power • other performance intensive application in browser • New languages will be run in Browser and NodeJS
WebAssembly vs JavaScript
HOW ENGINE WORKS JS WASM
VS
LOADING text format binary format
fast to load BYTE CODE
LOADING text format <script> blocking load binary format
LOADING text format <script> blocking load binary format fetch non blocking load
PARSING text format ambiguous syntax binary format
• const foo = x • const foo = x; • const foo = x > • const foo = x => • const foo = x ?
PARSE text format ambiguous syntax binary format S – expression
S-expression
S-expression
$0 10 Stack S-expression
$0 10 10 * $0 Stack S-expression
10 * $0 Stack S-expression
PARSE text format ambiguous syntax lazy parsing binary format S - expression
PARSING text format ambiguous syntax lazy parsing binary format S-expression no lazy parsing
CONCURRENT PARSING no concurrent parsing only researches
Proposed concurrent parser for JS
CONCURRENT PARSING no concurrent parsing only researches concurrent parsing
fast to load
CONCURRENT PARSING no concurrent parsing only researches concurrent parsing streaming API
1
3 2 1
5 4 3 2 1
CONCURRENT PARSING no concurrent parsing only researches concurrent parsing streaming API
COMPILITION + OPTIMIZATION main thread optimization after execution threads offline optimization
EXECUTION slow at start threads - workers no native threads GC 1.2 of native speed threads – workers native threads - future no GC now
EXECUTION many types access to DOM access to Web API access to JS functions only 4 primitive types no access to DOM no access to Web API access to JS functions
MODULES ES modules import/export function anywhere wasm modules strict structure
MODULES ES modules import/export function anywhere any function from scope wasm modules strict structure explicit import
INTERACTION WITH JAVASCRIPT We can use JS function in WASM and WASM function in JS
CAN WE USE WEBASSEMBLY? Yes we can
BUILD TARGET FOR • C/C++ • Rust • C# - Mono and CoreRT • Go • BrainFuck • Even more: https://github.com/appcypher/awesome-wasm-langs
VM • Blazor: NET code in the browser via the Mono runtime, which itself is compiled to WebAssembly and asm.js (Steve Sanderson) • TeaVM: transpiler of Java bytecode, to emit JavaScript and WebAssembly (Java, Kotlin, Scala) (Alexey Andreev) • Ocamlrun: OCaml bytecode interpreter for WebAssembly (Sebastian Markbåge)
USAGE SCENARIOS IN WEB • Write faster version of the specific application • Using existing C libraries in browser • Reuse algorithm from service side • Distribute whole application as wasm • Not only WEB: nodejs native modules with WASM+FFI
Ethereum Figma design Unity Google Earth Web Audio Modules
HOW TO START
SLOW START https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm •CMake •GCC or Visual Studio 2015 •Emscripten •Python 2.7.x
QUICK START WebAssembly Studio https://webassembly.studio/
ROADMAP • GC – garbage collector • Threads • Exception handling • Tools • More proposals are coming : Debug/Source maps, BigInt, SIMD and etc
EXAMPLES • Video editor: https://d2jta7o2zej4pf.cloudfront.net/ • Game: http://webassembly.org/demo/Tanks/ • Garden for Mozilla: https://s3.amazonaws.com/mozilla- games/ZenGarden/EpicZenGarden.html (Unreal Engine 4) • WebSight: https://websightjs.com/ • AutoCAD: https://web.autocad.com/
SUMMARY • WebAssembly is an emerging technology • Very promising and supported by all main browsers • Shortage of tools, libs, resources • Most WebAssembly features are part of the future specs
RESOURCES • Articles: https://hacks.mozilla.org/category/webassembly/ • Site: http://webassembly.org/ • Github: https://github.com/WebAssembly/ • Twitter: @WasmWeekly - WebAssemblyWeekly
THANKS!
QUESTIONS?

JS Fest 2018. Александр Скачков. WebAssembly vs JavaScript

Editor's Notes