Asm.js introduction
_by Oleksii Prohonnyi
OUT OF SCOPE
No information in this presentation
 WebAssembly
 NaCl / PNaCl
 LLVM
 JavaScript Engines Performance
 AOT compilation process
 OdinMonkey by Mozilla
 Chakra by Microsoft
 V8 by Google
HISTORY
History
 JavaScript is one of the world’s most popular programming languages.
 Back in the day, transpilers (source-to-source compilers) were used to hide
some of JavaScript’s ugly parts, e.g. CoffeeScript, ClojureScript,
TypeScript.
 Transpilers mainly use an existing language (such as C or C++) or, they
define a new language (like CoffeeScript).
 Then, instead of writing JavaScript, you can develop in this other language
that the transpiler converts to JavaScript.
Compiling to JavaScript: Nothing New!
 Google Web Toolkit (GWT), Java to JS
 pyjamas, Python into JS
 Opal, Ruby to JavaScript
 JSIL, .NET to JavaScript
 Scala.js, Scala to JavaScript
 etc.
OVERVIEW
What is asm.js?
 asm.js is a strict subset of JavaScript.
 asm.js takes advantage of some low level JavaScript features like Typed
Arrays.
 It does not use any plugins or modules to run the JavaScript code, making
it backward compatible.
 http://asmjs.org/
HOW IT WORKS
JavaScript vs asm.js
 JavaScript
 asm.js
GENERATORS
Emscripten
 Emscripten is an LLVM-to-JavaScript compiler.
 Emscripten accepts LLVM bytecode and converts them to asm.js
JavaScript.
 Clang could be used to convert C/C++ codes to LLVM.
 http://kripken.github.io/emscripten-site/
Code generation process
Code generation process
1. Create a C/C++ app.
2. Compile it using Clang to generate LLVM bytecode.
3. Pass the bytecode to Emscripten to get the JavaScript code.
 Emscripten performs the last two steps itself.
BROWSER SUPPORT
Browser support
 Mozilla Firefox implements asm.js-specific optimizations, starting with
Firefox 22. OdinMonkey, Mozilla's asm.js ahead-of-time compiler used in
Firefox.
 Microsoft is implementing support for asm.js in Chakra, the JavaScript
engine used by Microsoft Edge, performing validation to produce highly
optimized JIT code.
 The optimizations of Google Chrome's V8 JavaScript engine in Chrome 28
made asm.js benchmarks more than twice as fast as prior versions of
Chrome, although Chrome's V8 does not use ahead-of-time compilation.
Browser support
EXAMPLE
JavaScript snippet
C++ snippet
Code generation
 Install Emscripten (https://github.com/kripken/emscripten/wiki).
 Convert C++ code to asm.js code:
“./emcc -O1 -s ASM_JS=1 ./hello_world.cpp”
 Code execution with node.js:
“node ./a.out.js”
 Download Clang (http://clang.llvm.org/)
 Convert C++ code to bytecode:
“clang ./hello_world.cpp”
Generation result
Performance comparison
REFERENCES
See more
 http://asmjs.org/
 https://github.com/kripken/emscripten
 Big Web App? Compile It! (Alon Zakai / Mozilla)
 Asm.js JavaScript compile target (John Resig / Blog)
 Understanding asm.js (Sitepoint)
 From asm.js to WebAssembly (Brendan Eich / Blog)
 Mozilla Hacks
Oleksii Prohonnyi
facebook.com/oprohonnyi
linkedin.com/in/oprohonnyi

Asm.js introduction

  • 1.
  • 2.
  • 3.
    No information inthis presentation  WebAssembly  NaCl / PNaCl  LLVM  JavaScript Engines Performance  AOT compilation process  OdinMonkey by Mozilla  Chakra by Microsoft  V8 by Google
  • 4.
  • 5.
    History  JavaScript isone of the world’s most popular programming languages.  Back in the day, transpilers (source-to-source compilers) were used to hide some of JavaScript’s ugly parts, e.g. CoffeeScript, ClojureScript, TypeScript.  Transpilers mainly use an existing language (such as C or C++) or, they define a new language (like CoffeeScript).  Then, instead of writing JavaScript, you can develop in this other language that the transpiler converts to JavaScript.
  • 6.
    Compiling to JavaScript:Nothing New!  Google Web Toolkit (GWT), Java to JS  pyjamas, Python into JS  Opal, Ruby to JavaScript  JSIL, .NET to JavaScript  Scala.js, Scala to JavaScript  etc.
  • 7.
  • 8.
    What is asm.js? asm.js is a strict subset of JavaScript.  asm.js takes advantage of some low level JavaScript features like Typed Arrays.  It does not use any plugins or modules to run the JavaScript code, making it backward compatible.  http://asmjs.org/
  • 9.
  • 10.
    JavaScript vs asm.js JavaScript  asm.js
  • 11.
  • 12.
    Emscripten  Emscripten isan LLVM-to-JavaScript compiler.  Emscripten accepts LLVM bytecode and converts them to asm.js JavaScript.  Clang could be used to convert C/C++ codes to LLVM.  http://kripken.github.io/emscripten-site/
  • 13.
  • 14.
    Code generation process 1.Create a C/C++ app. 2. Compile it using Clang to generate LLVM bytecode. 3. Pass the bytecode to Emscripten to get the JavaScript code.  Emscripten performs the last two steps itself.
  • 15.
  • 16.
    Browser support  MozillaFirefox implements asm.js-specific optimizations, starting with Firefox 22. OdinMonkey, Mozilla's asm.js ahead-of-time compiler used in Firefox.  Microsoft is implementing support for asm.js in Chakra, the JavaScript engine used by Microsoft Edge, performing validation to produce highly optimized JIT code.  The optimizations of Google Chrome's V8 JavaScript engine in Chrome 28 made asm.js benchmarks more than twice as fast as prior versions of Chrome, although Chrome's V8 does not use ahead-of-time compilation.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
    Code generation  InstallEmscripten (https://github.com/kripken/emscripten/wiki).  Convert C++ code to asm.js code: “./emcc -O1 -s ASM_JS=1 ./hello_world.cpp”  Code execution with node.js: “node ./a.out.js”  Download Clang (http://clang.llvm.org/)  Convert C++ code to bytecode: “clang ./hello_world.cpp”
  • 22.
  • 23.
  • 24.
  • 25.
    See more  http://asmjs.org/ https://github.com/kripken/emscripten  Big Web App? Compile It! (Alon Zakai / Mozilla)  Asm.js JavaScript compile target (John Resig / Blog)  Understanding asm.js (Sitepoint)  From asm.js to WebAssembly (Brendan Eich / Blog)  Mozilla Hacks
  • 27.