Do things faster & better
with WebAssembly
@sendilkumarn
Sendil Kumar
• Full stack developer @Xebialabs
• Core dev team member @JHipster
• Team member @webpack
• Part of rust-wasm Working Group
• Big open source lover & enthusiast
@sendilkumarn
You can reach me at
C / C++ devs ?
Rust devs ?
What does Rust / C++
offer?
Faster
More
control
SafetyTypes
Closer to
machine
JS devs ?
Fullstack JS devs ?
What does JS offer?
Try to be
faster
Easier to
read
Easier to
writeTypes?
Far from
machine
What you do to increase
the Performance of your
App ?
Minify
Async & Defer
Reduce dom
manipulation
Profile
JS Engine based
optimisations
Immutability
Type system
Code splitting
Service worker
Let us do a
performance question
Which has higher
performance?
function test() {
let arr = [];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
}
function test() {
let arr = [];
arr[0] = 1;
arr[2] = 3;
arr[1] = 2;
}
Which has higher
performance?
function test() {
let arr = [];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
}
function test() {
let arr = [];
arr[0] = 1;
arr[2] = 3;
arr[1] = 2;
}
Which has higher
performance?
let arr = new Array(3);let arr = [];
Which has higher
performance?
let arr = []; let arr = new Array(3);
But why 😱?
Packed & Holey Array
https://v8project.blogspot.nl/2017/09/elements-kinds-in-v8.html
Uncertainties😕🤔
Most of the profiling, needs you to know
more about the internals of JS engine.
Optional
Arguments
Packed/Holey
Array
Monomorphisation
Micro-
benchmarking
Engine based
tunings
Known engine
shortcomings
Time-
consuming
Uncertainty in
performance & results
⚙ Maintainability ⚙
How better will it be to have
the consistency & speed of
native code in browsers?
WebAssembly
Binary encoded - Stack machine
In other words, you can compile
your native code and make
them run in your browser.
🚀 Fast & Efficient
🚀 Fast & Efficient
🔐 Safe
🚀 Fast & Efficient
🔐 Safe
🧐 Debuggable
🚀 Fast & Efficient
🔐 Safe
🧐 Debuggable
🕸 Open web platform
“WebAssembly is not a
replacement of JAVASCRIPT”
integrating well and being symbiotic
How JS works now inside
JS Engine
PARSE
COMPILE
OPTIMIZE
Re-OPTIMIZE
EXECUTE
GC
Javascript AST
PARSE
COMPILE
OPTIMIZE
Re-OPTIMIZE
EXECUTE
GC
Profiler
🔥
COMPILE 0101010101010
PARSE
COMPILE
OPTIMIZE
Re-OPTIMIZE
EXECUTE
GC
Profiler
🔥🔥🔥🔥🔥
PARSE
COMPILE
OPTIMIZE
Re-OPTIMIZE
EXECUTE
GC
Profiler
PARSE
COMPILE
OPTIMIZE
Re-OPTIMIZE
EXECUTE
GC
Profiler
🚮
How WASM Works?
DECODE
COMPILE
EXECUTE
01010101001010 0101011001111000
DECODE
COMPILE
EXECUTE
0101
0110
0111
1000
0101011001111000
DECODE
COMPILE
EXECUTE
0111
1000
0110 5
Simple Example in
Rust
#[no_mangle]
pub extern fn add_one(x: i32) -> i32{
x+1
}
RUST
rustc +nightly 
— —target wasm32-unknown-unknown 
-O test.rs
WASM
Raw binary format
WASM
MODULE
IMPORT
TABLE MEMORY
GLOBAL
EXPORT
ELEMENT
DATA
START
FUNCTION &
CODE
Let us see what is inside
Module
WASM
IMPORT
EXPORT
FUNCTION &
CODE
(import …)
(export …)
(func …)
Anything that you want to import inside WASM
Anything that you want to export outside WASM
Actual Binary code to execute
WASM
MEMORY
GLOBAL
START (start $start_function)
first function to get loaded & run in the module
(global type mutability expr)
(memory init-size max-size(opt))
WASM
TABLE
ELEMENT
DATA Data -> linear array of memory indexes
GC references, raw OS handles, or native pointers
Allows module to initialise the elements
(module
(type $t0
(func (param i32) (result i32)))
(func $add_one (export “add_one”)
(type $0) (param $p0 i32) (result i32)
get_local $p0
i32.const 1
i32.add
)
(table $T0 0 anyfunc)
(memory $memory (export “memory”) 17)
(data (i32.const 4) “10001000”))
WAST
(module )
Module
(type $t0
(func
(param i32)
(result i32)
)
)
Type
(func $add_one
(export “add_one”)
(type $0)
(param $p0 i32)
(result i32)
get_local $p0
i32.const 1
i32.add
)
Actual function
(table $T0 0 anyfunc)
Table
(memory $memory
(export “memory”)
17)
Memory
(data
(i32.const 4)
“10001000”)
)
Data
How to load them?
Using fetch…
Using compile streaming…
Using instantiate streaming…
⚠Few browsers yet to support
Streaming compilation
speeds up the wasm
performance by 10-15 times
Streaming compilation will be coming to JS… 🎉 🎉 🎉 🎉 🎉 🎉
ESM spec for WASM
⚠Still in very early stages, and will be implemented
Demo Time ⏱
Future 🎠
🦄 Garbage Collection
🦄 DOM Manipulation
🦄 Bulk memory operations
🦄 Threads
🦄 Exception handling
Questions?
Happy Hacking 😎

Do things faster and better with WebAssembly - Sendil Kumar Nellaiyapen - Codemotion Rome 2018