3 ms·
> compiling from ECMAScript to WebAssembly would be a great boon in production. That's not going to work well, I'm afraid. JavaScript is a highly-dynamic lang
by azakai 11y ago
> compiling from ECMAScript to WebAssembly would be a great boon in production.
That's not going to work well, I'm afraid.
JavaScript is a highly-dynamic language, and as a result, requires JITs that do runtime type profiling, type inference, and type-specialized code generation, in order to be fast.
WebAssembly is a low-level, statically typed language. It's like a portable C with a small binary footprint.
Even if you annotate and/or detect some of the JavaScript types at compile time, if you don't have them all, that means compiling JavaScript to WebAssembly will result in code that everywhere does "if this type, do that; if that type, do this". That's going to be bigger in code size than JavaScript currently is. It's also going to run much slower.
If you did type every single thing in your JavaScript program, somehow, then that might work well. But that's not really JavaScript anymore (or even TypeScript, etc.).
- bcheung 11y agoWebAssembly is a binary representation of the Javascript AST. Optimizations usually happen AFTER it is parsed. That's all WebAssembly is -- the parsed result. Targeting WebAssembly from Javascript is the most natural fit that could possibly exist.
- azakai 11y agoNot quite. WebAssembly is not a representation of general JS. It can just represent a small subset of it, very similar to the asm.js subset.
- bcheung 11y agoGood to know. So it doesn't implement any of the ES6 concepts then I take it.
- esailija 11y agoThe format is just an AST in general, nothing to do with JavaScript. You can even see all the possible instructions here to confirm [1]. 1. https://github.com/WebAssembly/design/blob/master/AstSemantics.md https://github.com/WebAssembly/design/blob/master/AstSemanti...
- xigency 11y agoWhat about this JavaScript: https://raw.githubusercontent.com/gregtour/gameboy/master/JS/gameboy.js https://raw.githubusercontent.com/gregtour/gameboy/master/JS... It's not asm.js, it isn't C (but it was ported from C), but it's JavaScript and it has no dynamic allocations. No variable uses more than one type. I could hand fashion asm.js and WebAssembly code and tell you which one is faster. That's the only answer to whether a JavaScript to WebAssembly compiler would be worthwhile.