61

I have read the question How to test and develop with asm.js?, and the accepted answer gives a link to http://kripken.github.com/mloc_emscripten_talk/#/.

The conclusion of that slide show is that "Statically-typed languages and especially C/C++ can be compiled effectively to JavaScript", so we can "expect the speed of compiled C/C++ to get to just 2X slower than native code, or better, later this year".

But what about non-statically-typed languages, such as regular JavaScript itself? Can it be compiled to asm.js?

7
  • 1
    I don't think so. Why would you need that? JS engines are optimized to execute regular, full JavaScript - not only the strict asmjs subset.
    – Bergi
    Mar 25, 2013 at 23:36
  • 7
    @Bergi But if asmjs code runs faster than regular JS, then it makes sense to compile JS to asmjs, no?
    – Oriol
    Mar 25, 2013 at 23:49
  • 3
    See this link: jlongster.com/Compiling-LLJS-to-asm.js,-Now-Available-
    – gkiely
    Mar 28, 2013 at 2:05
  • @Grant: Nice link, thanks! But don't forget, "In order to compile to asm.js, [the author] had to remove the ability to mix normal untyped javascript code, so it all has to be typed".
    – Bergi
    Apr 1, 2013 at 10:27
  • 1
    Take a look at developers.google.com/v8/experiments#strong-mode. This is essentially a proposal that places various restrictions on how you can use Javascript, with the aim of enabling various performance optimizations in the JS VM. This will bring the performance of hand-written "strong mode" JS closer and closer to asm.js levels.
    – joelpt
    Apr 19, 2016 at 3:08

7 Answers 7

34

Can JavaScript itself be compiled to asm.js?

Not really, because of its dynamic nature. It's the same problem as when trying to compile it to C or even to native code - you actually would need to ship a VM with it to take care of those non-static aspects. At least, such a VM is possible:

js.js is a JavaScript interpreter in JavaScript. Instead of trying to create an interpreter from scratch, SpiderMonkey is compiled into LLVM and then emscripten translates the output into JavaScript.

But if asmjs code runs faster than regular JS, then it makes sense to compile JS to asmjs, no?

No. asm.js is a quite restricted subset of JS that can be easily translated to bytecode. Yet you first would need to break down all the advanced features of JS to that subset for getting this advantage - a quite complicated task imo. But JavaScript engines are designed and optimized to translate all those advanced features directly into bytecode - so why bother about an intermediate step like asm.js? Js.js claims to be around 200 times slower than "native" JS.

And what about non-statically-typed languages in general?

The slideshow talks about that from …Just C/C++? onwards. Specifically:

Dynamic Languages

Entire C/C++ runtimes can be compiled and the original language interpreted with proper semantics, but this is not lightweight

Source-to-source compilers from such languages to JavaScript ignore semantic differences (for example, numeric types)

Actually, these languages depend on special VMs to be efficient

Source-to-source compilers for them lose out on the optimizations done in those VMs

5
  • 3
    But if js.js SpiderMonkey (C/C++) translated to JavaScript, I don't see the relation between js.js and compiling JavaScript to asm.js? It makes sense for me that a JavaScript interpreter is slower than a C/C++ one.
    – Oriol
    Mar 26, 2013 at 16:27
  • 1
    OK, that might have been a bit inexact - js.js would not compile to asmjs code, but execute code in asmjs context. What I meant was that you'd need to find a way to break JavaScript down to asmjs-compatible instruction (e.g. in C), and actually the only way I've seen needs to ship a VM with it
    – Bergi
    Mar 26, 2013 at 16:40
  • is it possible to run js code through some sort of compiler that takes code convert it into typed version therefore eliminating the need to have VM. Nov 28, 2013 at 3:45
  • 1
    @Muhammad: Please re-read the first section of my answer. Btw, JS is already "typed".
    – Bergi
    Nov 28, 2013 at 5:33
  • 1
    If JS's dynamic features could be easily compiled to static binaries, browsers would be shipping with AOT compilers for standard JS, which would make translating JS to asm.js redundant anyway.
    – lcmylin
    Jul 11, 2015 at 23:20
12

In response to the general question "is it possible?" then the answer is that sure, both JavaScript and the asm.js subset are Turing complete so a translation exists.

Whether one should do this and expect a performance benefit is a different question. The short answer is "no, you shouldn't." I liken this to trying to compress a compressed file; yes, it is possible to run the compression algorithm, but in general you should not expect the resulting file to be smaller.

The short answer: The performance cost of dynamically-typed languages comes from the meaning of the code; a statically-typed program with an equivalent meaning would carry the same costs.

To understand this, it is important to understand why asm.js offers a performance benefit at all; or, more generally, why statically-typed languages perform better than dynamically-typed ones. The short answer is "run-time type checking takes time," and a longer answer would include the improved feasibility of optimizing statically-typed code. For example:

function a(x) { return x + 1; }
function b(x) { return x - 1; }
function c(x, y) { return a(x) + b(y); }

If x and y are both known to be integers, I can optimize function c to a couple of machine code instructions. If they could be integers or strings, the optimization problem becomes much harder; I have to treat these as string appends in some cases, and addition in other cases. In particular, there are four possible interpretations of the addition operation that occurs in c; it could be addition, or string append, or two different variants of coerce-to-string-and-append. As you add more possible types, the number of possible permutations grows; in the worst case for a dynamically-typed language, you have k^n possible interpretations of an expression involving n terms which could each have any number of k types. In a statically typed language, k=1, so there is always 1 interpretation of any given expression. Because of this, optimizers are fundamentally more efficient at optimizing statically-typed code than dynamically-typed code: There are fewer permutations to consider when searching for opportunities to optimize.

The point here is that when converting from dynamically-typed code to statically-typed code (as you'd be doing when going from JavaScript to asm.js), you have to account for the semantics of the original code. Meaning the type-checking still occurs (it's just now been spelled out statically-typed code) and all those permutations are still present to stifle the compiler.

1
  • One aside: You do see performance benefit from investing processor time during a compile step on JavaScript, e.g. with the Closure Compiler; partly this is because you have time to get much deeper into the type inference problem during a build than in-browser. In principle, I could imagine using asm.js as a more effective medium for the output of this step; that is, some of the useful info about types could get lost again when converting back to vanilla JS. In practice, my guess is that any such benefit would be unobservable.
    – killscreen
    May 11, 2015 at 0:28
11

A few facts about asm.js, which hopefully make the concept clear:

  1. Yes you can write the asm.js dialect by hand.

    If you did look at the examples for asm.js, they are very far from being user friendly. Obviously Javascript is not the front end language for creating this code.

  2. Translating vanilla Javascript to asm.js dialect is not possible.

    Think about it - if you already could translate standard Javascript in a fully statically manner, why would there be a need for asm.js? The sole existance of asm.js means that the Javascript JIT people at some people gave up on their promise that Javascript will get faster without any effort from the developer.

    There are several reasons for this, but let's just say it would be really hard for the JIT to understand a dynamic language as good as a static compiler. And then probably for the developers to fully understand the JIT.

In the end it boils down to using the right tool for the task. If you want static, very performant code, use C / C++ ( / Java ) - if you want a dynamic language, use Javascript, Python, ...

6
  • 11
    Any Turing complete language can be translated to any other Turing complete language. Sep 13, 2013 at 13:09
  • 11
    Just because you could do something does not mean that it is automatically a sensible approach.
    – abergmeier
    Nov 17, 2013 at 10:03
  • 3
    Sensible or not, your assertion that it is "not possible" is false. It's possible but (based on other answers) there is no reason to expect any performance benefit. By analogy, if a child is reaching out to touch a hot stove, telling them "it's impossible to touch that stove, don't bother trying" is neither correct nor useful advice.
    – killscreen
    May 10, 2015 at 22:22
  • 1
    I'll add a caveat that interpretation isn't the same as compilation. Compiling to a subset like ASM.js would include optimizations that are just not possible from on-the-fly interpreting. Flow and TypeScript demonstrate this concept by understanding common idioms people use during development. Furthermore, if the programmer is willing to give up some language features, the compiler's job could just be reduced to removing syntactic sugar.
    – aindurti
    Aug 20, 2015 at 3:01
  • @abergmeier I have a question here: If static typing is so good in terms of reliability, performance, etc that we have all these statically typed languages transpiled into javascript, such as coffescript, typescript, asm.js. Why haven't javascript evolved into a Statically typed language yet? Is it a reasonable question to ask? Feb 11, 2019 at 18:51
3

asm.js has been created by the need of have a small subset of javascript which can be easily optimized. If you can have a way to convert javascript to javascript/asm.js, asm.js is not needed anymore - that method can be inserted in js interpreters directly.

2

In theory, it is possible to convert / compile / transpile any JavaScript operation to asm.js if it can be expressed with the limited subset of the language present in asm.js. In practice, however, there is no tool capable of converting ordinary JavaScript to asm.js at the moment (June, 2017).

Either way, it would make more sense to convert a language with static typing to asm.js, because static typing is a requirement of asm.js and the lack thereof one of the features of ordinary JavaScript that makes it exceptionally hard to compile to asm.js.

Back in 2013, when asm.js was hot, there has been an attempt to compile a statically typed language similar to JavaScript, but both the language and the compiler seem to have been abandoned.

Today, in 2017, JavaScipt subsets TypeScript and Flow would be suitable candidates for conversion to asm.js, but the core dev teams of neither language is interested in such conversion. LLJS had a fork that could compile to asm.js, but that project is pretty much dead. ThinScript is a much more recent attempt and is based on TypeScript, but it doesn't appear to be active either.

So, the best and easiest way to produce asm.js code is still to write your code in C/C++ and convert / compile / transpile it. However, it remains to be seen whether we'll even want to do this in the forseeable future. Web Assembly may soon replace asm.js altogether and there's already popping up TypeScript-like languages like TurboScript and AssemblyScript that convert to Web Assembly. In fact, TurboScript was originally based on ThinScript and used to compile to asm.js, but they appear to have abandoned this feature.

1
  • Thank you for your very detailed answer!
    – Andrew
    Nov 16, 2017 at 3:24
2

It may be possible to convert regular JavaScript to asm.js by first compiling it to C or C++, and then compiling the generated code to asm.js using Emscripten. I'm not sure if this would be practical, but it's an interesting concept nonetheless.

There is also a compiler called NectarJS that compiles JavaScript to WebAssembly and ASM.js.

1

check this http://badassjs.com/post/43420901994/asm-js-a-low-level-highly-optimizable-subset-of

basically you need check that your code would be asm.js compatible (no coercion or type casting, you need to manage the memory, etc). The idea behind this is write your code in javascript, detect the bottle neck and do the changes in your code for use asm.js and aot compilation instead jit and dynamic compilation...is a bit PITA but you can still use javascript or other languages like c++ or better..in a near future, lljs.....

1
  • Thinking writing the code in TypeScript would really help keep the development time down.
    – Steve
    Dec 14, 2013 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.