KEMBAR78
WebAssembly vs JavaScript: What is faster? | PDF
WEBASSEMBLY VS JAVASCRIPT:
WHAT IS FASTER?
Oleksandr Skachkov @alSkachkov
.
• Itera’s FrontEnd Tech Lead
• WebKit contributor:
• Arrow function
• Async iterator & generators
• Promise.finally
• Twitter: @alSkachkov
ABOUT ME
• What was before
• WebAssembly key features
• Is WebAssembly really fast?
• WebAssembly now
JavaScript one of the most popular language
TRIES TO REPLACE JS
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
WAT?
fast to load
Byte code
Will WebAssembly replace JavaScript?
Not now J
but WebAssembly will transform web
WASM WILL BRING TO WEB
•New languages
•Predictable high performance
WebAssembly key features
HOW ENGINE WORKS
JS
WebAssembly
LOADING
LOADING
text format
<script>
blocking load
binary format
fetch
non blocking load
PARSING
text format
lazy parsing
main thread
no streaming API
binary format
S – expression
background threads
streaming API
3 2 1
5 4 3 2 1
5 4 3 2 1
PARSING
text format
lazy parsing
main thread
no streaming API
binary format
S – expression
background threads
streaming API
COMPILATION + OPTIMIZATION
main thread
optimization after
execution
background threads
offline optimization
EXECUTION
slow at start
threads - workers
no native threads
GC
close to native speed
threads – workers
native threads - future
no GC now
EXECUTION
many types
access to JS functions
access to DOM
access to Host API
only 4 primitive types
access to JS functions
no access to DOM
no access to Host API
INTERACTION WITH JAVASCRIPT
We can use JS function in WASM and WASM function in JS
IS WEBASSEMBLY REALLY FAST?
WHAT IS PERFROMANCE FOR US
• LOAD
• COMPILE
• EXECUTION
WebAssembly module is downloaded over Network
Size really matter
WASM module size expected to be smaller than js, but
SIZE OF “HELLO WORLD”
SIZE OF “HELLO WORLD”
• JavaScript – 98 bytes
• pure wasm – wasm 110 bytes + js 321 bytes
• Rust ⇒ wasm – wasm 16Kb - 600Kb + js 2 Kb
• C ⇒ emcc ⇒ wasm – wasm 22 Kb + js 99 Kb
• C#(Blazor/Mono) ⇒ wasm ~ 2 Mb
• ReactJS 16.2.0 + React DOM – 97 KB
WebAssembly is expected to be fast in compiling
COMPILING
Let’s take module and find speed in Mb per second :
• JavaScript – 16.5 Mb
• WebAssembly – 12.4 Mb
COMPILING
Streaming compiling is expected even more faster
STREAMING COMPILING
SUMMARY
• Size of WebAssembly module depends on compiler
• WebAssembly compilation is slower to js in Mb/s
• WebAssembly has streaming compilation that compile module during network delays
• WebAssembly might be parsed out of main thread
WebAssembly is expected to be fast in execution
Simple cases
Speedup of the Math operations
SPEEDUP
times
Chrome 69
SPEEDUP
times
Firefox 62
SPEEDUP
times
Safari 11.11
What if call js function from WebAssembly
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
Fibonaccinumber
SLOWDOWNtimes
Complex case
Only 4 types
Source Map
Source Map
https://github.com/mozilla/source-map
SOURCE MAP
Crypto
CRYPTO - PBKDF2
PBKDF2 - Password-Based Key Derivation Function 2
• JS: asmcrypto.js https://github.com/asmcrypto/asmcrypto.js
• Native: Browser Web Crypto API
• WASM: Rust module with rust-crypto package
CRYPTO - PBKDF2
Convert images
Convert images
https://developer.mozilla.org/en-US/docs/WebAssembly/existing_C_to_wasm
Google Senior Developer Advocate @DasSurma
CONVERT IMAGES
• Canvas + JS:
From JPEG to BITMAP by Canvas API and to WEBP by JS (lib webpjs)
• Canvas + WASM:
From JPEG to BITMAP by Canvas API and to WEBP by WASM (lib libwebp)
• Full WASM: JPEG to WEBP by WASM (libs: libjpeg, libwebp)
CONVERT IMAGES
Times
Iterations
Why?
INTERACTION WITH JS
Performance Tab in Chrome Developer Tools
https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/
INTERACTION WITH JS
Performance Tab in Chrome Developer Tools
https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/
Can WebAssembly be faster to Host API?
sometimes, if we use WebWorkers
Can WebAssembly be faster to Host API?
sometimes, if we use WebWorkers
CONVERT IMAGES
Timetoconvert400imagesinseconds
Workers
CONVERT IMAGES
Timetoconvert400imagesinseconds
Workers
Why?
CONVERT IMAGES
• Canvas API works in main thread and blocks it
• WebAssembly does not block main thread
SUMMARY
• WebAssembly has very good performance on pure mathematics
• Call JS from WebAssembly lead to slow down of the performance
• Host API might be slower then WebAssembly in edge cases(!)
Is WebAssembly slow and useless
WHAT PROJECT FITS TO WEBASSEMLBY
• Requires a lot of computations
• Is developed with WebAssembly and high performance in mind
• Has small amount of interactions with JavaScript or DOM
• Has big C/C++ code base and high performance is not a goal
• Fast enough for Browser
WEBASSEMBLY RIGHT NOW
CAN WE USE WEBASSEMBLY?
Yes we can
How can we use WebAssembly?
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
• Secure your algorithms J
BUILD TARGET FOR
• C/C++
• Rust
• C# - Mono and CoreRT
• Go
• BrainFuck
• Even more: https://github.com/appcypher/awesome-wasm-langs
Ethereum Figma design
Unity Google Earth
Web Audio Modules
RESOURCES
• Articles:
• https://hacks.mozilla.org/category/webassembly/
• https://pspdfkit.com/blog/
• ategory/webassembly/
• Site: http://webassembly.org/
• Github: https://github.com/WebAssembly/
• Twitter: @WasmWeekly - WebAssemblyWeekly
EXAMPLES
• PDF Viewer: https://web-preview.pspdfkit.com/standalone
• 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/
THANKS!
QUESTIONS?

WebAssembly vs JavaScript: What is faster?