13

As far as I know, Asm.js is just a strict specification of JavaScript, it uses the JavaScript features and it's not a new language.

For instance, instead of using var a = e;, it offers var a = e|0;.

My question is, if asm.js is just a definition and can be achieved by changing the way one uses and declares variables and dynamic types, what does "use asm"; actually do? Is this necessary to put this string before declaring function's body or not?

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
  • 2
    It's a declarative that lets supported JS engines knows of your intent to provide asm.js optimized code, and will therefore attempt those optimizations. I think I remember hearing that V8 was implementing those optimizations without the declarative. Not sure about that though. – cookie monster May 03 '14 at 19:27
  • 1
    You should read about ASM here to better understand it. http://ejohn.org/blog/asmjs-javascript-compile-target/ – Rob M. May 03 '14 at 19:27
  • @cookiemonster I can't get your meaning by **optimizations**. This sort of optimizations created by the developer, am I right? Look at the bitwise operation that I wrote in the question. – Afshin Mehrabani May 03 '14 at 19:33
  • 1
    No, optimizations provided by the engine itself. Those optimizations require very specific syntax that needs to be written by the developer *(or more likely generated by a code generator)*. In an implementation that doesn't support such optimizations, the code will likely run slower than JS code written in the typical manner. – cookie monster May 03 '14 at 19:36
  • 1
    It's like "use strict", strict JS is also a subset of regular JS. So you tell the engine (in a backwards compatible way) that you are using that subset and slightly different rules can be followed – Ruan Mendes May 03 '14 at 19:45
  • @cookiemonster aha, and just to wrap up, using both bitwise operations (or other asm.js specifications) same as what I said in the question AND also using `"use asm";` will execute the given code faster. Otherwise, the code will be slower than regular JS code, right? – Afshin Mehrabani May 03 '14 at 19:45
  • Mostly. I don't know how much room for optimization there is in a single line of code, but for code that is processor intensive and can benefit from such optimizations, then yes, if the engine recognizes the "use asm" declarative, and if the code is properly written for asm.js, then yes it should run faster. And yes, it is very likely that the code written to target asm.js will take a performance hit in implementations that don't offer such optimizations. I say likely, because I don't want to make any absolute statements, but I think it'll generally be the case. – cookie monster May 03 '14 at 20:08

2 Answers2

10

Asm.js is a very strict subset of JavaScript, that is optimized for machines rather than humans. If you want your browser to interpret certain code as asm.js code, you need to create a module wherein the following conditions apply :

  • all code is fully statically typed and limited to the very restrictive asm.js subset of JavaScript
  • your module starts with the "use asm" pragma

Additionally, an asm.js module allows only up to three optional yet very specific parameters :

  • a standard library object, providing access to a subset of the JavaScript standard libraries
  • a foreign function interface (FFI), providing access to custom external JavaScript functions
  • a heap buffer, providing a single ArrayBuffer to act as the asm.js heap

So, your module should basically look like this :

function MyAsmModule(stdlib, foreign, heap) {
    "use asm";

    // module body...

    return {
        export1: f1,
        export2: f2,
        // ...
    };
}

The function parameters of your module allow asm.js to call into external JavaScript and to share its heap buffer with "normal" JavaScript. The exports object returned from the module allows external JavaScript to call into asm.js.

Leave out the "use asm", and your browser will not know that it should interpret your code as an asm.js module. It will treat your code as "ordinary" JavaScript. However, just using "use asm" is not enough for your code to be interpreted as asm.js. Fail to meet any of the other criteria mentioned hereabove, and your code will be also interpreted as "ordinary" JavaScript :

enter image description here

For more info on asm.js, see eg. John Resig's article from 2013 or the official specs.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
5

"use asm" is a pragma that tells the JavaScript engine specifically how to interpret it. Although it's valid JavaScript and can be used without the pragma, FireFox can perform additional optimizations to the Asm.js subset to increase performance. To do this, it must know that it is Asm.js.

Syntax
  • 2,073
  • 15
  • 15