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.
killscreen May 11 '15 at 12:12 2015-05-11 00:12
source share