Can ordinary JavaScript be converted to asm.js, or is it just to speed up statically typed low-level languages?

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

The conclusion of this slide show is that “statically typed languages ​​and especially C / C ++ can be efficiently compiled for JavaScript,” so we can “expect that the speed of compiled C / C ++ will only be 2 times slower than the native code. or better, at the end of this year. "

But what about non-statically typed languages ​​like regular JavaScript? Can it be compiled asm.js?

+58
javascript
Mar 25 '13 at 23:16
source share
7 answers

Can JavaScript itself be compiled in asm.js?

Not very, due to its dynamic nature. This is the same problem as compiling in C or even in your own code - you really need to send a VM with it to take care of those non-static aspects. At least such a virtual machine is possible:

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

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

No. asm.js is a fairly limited subset of JS that can easily be converted to bytecode. However, first you need to break all of the advanced JS functions into this subset to get this benefit - a pretty difficult imo task. But JavaScript engines are designed and optimized to translate all of these advanced features directly into bytecode - so why bother with an intermediate step like asm.js? Js.js claims to be about 200 times slower than native JS.

What about non-statically typed languages ​​in general?

The slideshow talks about this from ... Only C / C ++? and further. In particular:

Dynamic languages

You can compile whole C / C ++ and the source language is interpreted using the correct semantics, but it’s not easy

Source compilers from such JavaScript languages ​​ignore semantic differences (e.g. numeric types)

In fact, these languages ​​depend on special virtual machines to be efficient.

The source compilers for them lose the optimization performed in these virtual machines.

+34
Mar 26 '13 at 8:31
source share

In response to the general question "is this possible?" then the answer will be sure that both JavaScript and a subset of asm.js are Turing, so there is a translation.

Whether this is necessary to do and expect a performance advantage is another matter. Short answer: "No, not worth it." I compare this to trying to compress a compressed file; Yes, you can run the compression algorithm, but in general you should not expect the resulting file to be smaller.

Short answer: The cost of dynamically typed languages ​​comes from the value of the code; a statically typed program with an equivalent value will bear the same costs.

To understand this, it is important to understand why asm.js offers a performance advantage; or, more generally, why statically typed languages ​​work better than dynamically typed ones. The short answer is, "checking the runtime takes time," and the longer answer will include an improved ability to optimize 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 both known as integers, I can optimize the function c to a few machine code instructions. If they can be integers or strings, the optimization problem becomes much more complicated; I should consider them as adding rows in some cases, as well as in other cases. In particular, there are four possible interpretations of the addition operation, which occurs in c ; it can be adding or adding a string or two different options for coerce-to-string-and-append. When you add more possible types, the number of possible permutations increases; in the worst case, for a dynamically typed language, you have possible interpretations of an expression containing n terms, each of which can 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 significantly more efficient at optimizing statically typed code than dynamically typed code: when searching for opportunities for optimization, fewer permutations are required.

The point here is that when converting from dynamically typed code to statically typed code (as well as when switching from JavaScript to asm.js) you need to consider the semantics of the source code. This means that type checking is still in progress (statically typed code has now been written), and all these permutations are still present to drown out the compiler.

+12
May 11 '15 at
source share

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

  • Yes, you can write asm.js dialect manually.

    If you looked at examples for asm.js, they are very far from usability. Obviously, Javascript is not an interface for creating this code.

  • Translation of Javascript vanilla into asm.js dialect not .

    Think about it - if you can already fully translate standard Javascript into completely static, why do you need asm.js? The only existence of asm.js means that Javascript for some people has given up on its promise that Javascript will work faster without any developer effort.

    There are several reasons for this, but just say that for JIT it will be valid to understand a dynamic language as well as a static compiler. And then, probably, the developers will fully understand JIT.

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

+11
Mar 27 '13 at 16:30
source share

asm.js was created because of the need to have a small subset of javascript that can be easily optimized. If you have a way to convert javascript to javascript / asm.js, asm.js is no longer needed - this method can be directly inserted into js interpreters.

+3
May 9 '14 at 10:37
source share

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

In any case, it would be more appropriate to convert the language with static typing to asm.js, since static typing is a requirement of asm.js and the absence of it is one of the features of regular JavaScript, which makes it extremely difficult to compile in asm.js.

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

Today, in 2017, subsets of JavaScipt TypeScript and Flow would be suitable candidates for conversion to asm.js, but the main development teams of not a single language are interested in such a conversion. LLJS had a fork that could compile asm.js, but this project is pretty much dead. ThinScript is a much more recent attempt and is based on TypeScript, but it is also not active.

So, the best and easiest way to create asm.js code is to write code in C / C ++ and convert / compile / rewrite it. However, it remains to be seen whether we want to do this in the foreseeable future. Web assembly may soon replace asm.js as a whole, and TypeScript-like languages ​​such as TurboScript and AssemblyScript are already appearing there, which will be converted to web assembly. In fact, TurboScript was originally based on ThinScript and was used for compilation in asm.js, but they seem to have abandoned this function.

+2
Jun 22 '17 at 14:11
source share

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

basically you need to check that your code will be compatible with asm.js (without forcing or tick, you need to manage memory, etc.). The idea behind this is to write code in javascript, detect the neck of the bottle, and make changes to your code to use asm.js and compile aot instead of jit and dynamically compile ... a bit of PITA, but you can still use javascript or other languages ​​like C ++ or better .. in the near future, lljs .....

+1
Mar 25 '13 at 23:48
source share

It may be possible to convert plain JavaScript to ams.js by first compiling it in C or C ++ and then compiling the generated code in asm.js using Emscripten . I’m not sure that it will be practical, but, nevertheless, this is an interesting concept.

More recently, I found another tool called NectarJS that compiles JavaScript into WebAssembly and ASM.js.

+1
Oct 30 '16 at 15:44
source share



All Articles