Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between asm.js and WebAssembly?

I have been reading about asm.js and WebAssembly recently:

http://ejohn.org/blog/asmjs-javascript-compile-target/

https://brendaneich.com/2015/06/from-asm-js-to-webassembly/

I am still confused about a few things:

  1. Is asm.js code compiled in time and run? Compiled into what?
  2. Other than asm.js being text and wasm (web assembly) being binary, what are the differences between the 2?
  3. What does this mean for other scripting languages, running in the browser? Take python for example, is it going to be
    • python code compiled to wasm? or
    • python interpreter (Cpython) compiled into wasm and interpret python?
like image 714
NeoWang Avatar asked Jul 19 '15 15:07

NeoWang


People also ask

What is ASM js used for?

js is a subset of JavaScript designed to allow computer software written in languages such as C to be run as web applications while maintaining performance characteristics considerably better than standard JavaScript, which is the typical language used for such applications.

Is WebAssembly the same as assembly?

Be readable and debuggable — WebAssembly is a low-level assembly language, but it does have a human-readable text format (the specification for which is still being finalized) that allows code to be written, viewed, and debugged by hand.

Is WebAssembly better than JavaScript?

In one recent study, a developer discovered Wasm is faster than JavaScript across three desktop browsers on desktop computers and smartphones. Wasm is 1.15-1.67 times faster than JavaScript on Google Chrome on a desktop. Wasm is 1.95-11.71 times faster than JavaScript on Firefox on a desktop.

Will JavaScript be replaced by WebAssembly?

Still, WebAssembly will allow developers to move intensive UI-blocking work outside of the browser. It will result in better performing web applications that run on newer and older hardware more smoothly. So, to answer the question (if the answer wasn't obvious enough), WebAssembly will not replace Javascript.


2 Answers

asm.js is a subset of JS with "highly optimizable" instructions. Basically you can declare the type (int, float) and the js engine (in the browsers but also the node.js one) will execute the instructions faster. It has benefits if your app does a lot of calculation or graphic if used together with WebGL.

web assembly is a binary format for JS, all JS, not only asm.js. It's not a bytecode, it's a binary encoding of the AST that the parser calculates. It has 2 big benefits:

  • the JS engine can skip the parsing step
  • it's much more compact than the JS original source

We already can write code for browsers that isn't JS: EMSCripten can compile c++ code in JS code. Other transcompiler are already available to compile your code into JS. Using asm.js that code can run faster when it does math. Using web assembly that code will be more compact and the browser will be able to process it faster (because it will be able to skip the parsing). You won't have a new plugin to load like DirectX, JavaApplets, Flash or Silverlight because everything will run in the JS sandbox.

like image 182
cristian v Avatar answered Oct 23 '22 17:10

cristian v


Is asm.js code compiled in time and run? Compiled into what?

asm.js is regular javascript code, and is compiled into bytecode by the JS interpreter as always. However, an interpreter with asm support is supposed to do ahead-of-time compilation, and possibly to generate more efficient code representation because of the static typing. See http://asmjs.org/ for details.

what are the differences between asm and wasm (other than text vs binary)?

None, for now. wasm is supposed to be backwards-compatible, compilable to asm (which again is executable as normal JS). It might however be extended with more features in the future as support for it grows.

What does this mean for other scripting languages, running in the browser?

The latter, rather, as Python still needs to be interpreted. Scripting languages that don't need an interpreter can of course be directly compiled to (w)asm, given that there is a compiler (chain) that supports it as a target.

like image 21
Bergi Avatar answered Oct 23 '22 16:10

Bergi