asm.js
Platform independent | |
---|---|
Website | asmjs |
Influenced by | |
JavaScript | |
Influenced | |
WebAssembly |
asm.js is a
asm.js consists of a
Mozilla Firefox was the first web browser to implement asm.js-specific optimizations, starting with version 22.[3]
asm.js is superseded by WebAssembly. See § Deprecation below.
Design
asm.js enables significant performance improvements for web applications, but does not aim to improve the performance of hand-written JavaScript code, nor does it enable anything other than enhanced performance.
It is intended to have performance characteristics closer to that of native code than standard JavaScript by limiting language features to those amenable to
Code generation
asm.js is not typically written directly: instead, as an intermediate language, it is generated through the use of a compiler that takes source code in a language such as C++ and outputs asm.js.
For example, given the following C code:
int f(int i) {
return i + 1;
}
Emscripten would output the following JS code:
function f(i) {
i = i|0;
return (i + 1)|0;
}
Note the addition of |0
and the lack of type specifiers. In JavaScript, bitwise operators convert their operands to 32-bit signed integers and give integer results. This means that a
Here is another example to calculate the length of a string:
size_t strlen(char *ptr) {
char *curr = ptr;
while (*curr != 0) {
curr++;
}
return (curr - ptr);
}
This would result in the following asm.js code:
function strlen(ptr) {
ptr = ptr|0;
var curr = 0;
curr = ptr;
while ((MEM8[curr>>0]|0) != 0) {
curr = (curr + 1)|0;
}
return (curr - ptr)|0;
}
In the generated code, the variable MEM8 is actually a byte-by-byte "view" of a typed buffer, which serves as the "heap" of the asm.js code.
Performance
Since asm.js runs in a browser, the performance heavily depends on both the browser and hardware. Preliminary benchmarks of C programs compiled to asm.js are usually within a factor of 2 slower than native compilation with Clang.[6]
Much of this performance gain over normal JavaScript is due to 100%
Mozilla's benchmark from December 2013 showed significant improvements: "Firefox with
Implementations
The Emscripten project provides tools that can be used to compile C and C++ codebases (or any other languages that can be converted to LLVM IR) into asm.js.[2]
All browsers with support for
Math.fround()
), older browsers lacking those features may encounter problems.
Some browser implementations are especially optimised for asm.js:
- IonMonkey, the JIT compiler of SpiderMonkey.
- Microsoft was implementing support for asm.js in Chakra, the JavaScript engine used by Microsoft Edge Legacy, performing validation to produce highly optimised JIT code.[8]
- The optimizations of Google Chrome's V8 JavaScript engine in Chrome 28 made asm.js benchmarks more than twice as fast as prior versions of Chrome,[9] although Chrome's V8 does not use ahead-of-time compilation.
Adoption
Almost all of the current applications based on asm.js are C/C++ applications compiled to asm.js using Emscripten or Mandreel. With that in mind, the kind of applications that are going to target asm.js in the near future are those that will benefit from the portability of running in a browser but which have a level of complexity for which a direct port to JavaScript would be infeasible.
So far, a number of
Some of them are given below.Programming languages
- Rust: targets Emscripten
- Perl: port of (micro)perl-5.16.3[11]
- Python – port of CPython[12]
- Ruby – port of Ruby[13]
Application frameworks
- pepper.js: Ports of miscellaneous PNaCl apps (earth, voronoi, bullet, etc.)[14]
Programs and libraries
- OpenGL, SDL, and SDL2[16]
- Vim (Vi IMproved)[17]
- font rendering in JavaScript, using FreeType[18]
- SQLite[19]
- GNU Privacy Guard[20]
- ctags[21]
- gnuplot[22]
- Graphviz[23]
- zlib[24]
Game engines
Games
- PrBoom, which is based on the open source Doom code[30]
- SuperTux[31]
- Dune II via OpenDune[32]
- BananaBread based on Cube 2[33]
- Every game in the Democracy 3)
Emulators
- EM-DOSBox: an Emscripten port of DOSBox[35]
- Start9.io: a web emulation platform targeting multiple gaming architectures
- JSMESS: a port of the MESS emulator for many game consoles and computer systems[36]
Mathematics
- HTML5 Fractal Playground[37] – draws iterating-function generated fractals, such as the Mandelbrot set.
Deprecation
asm.js is mostly rendered obsolete with the introduction of WebAssembly (wasm), which has a bytecode format that is faster to parse.[38] Efforts to extend JavaScript with more low-level features like SIMD.js has also been suspended since 2017.[39]
asm.js remains useful primarily as a "fallback" for wasm, through a program written by the WebAssembly organization that converts wasm to asm.js. There is no dedicated converter from asm.js to wasm, but TypeScript-to-wasm compilers can be partially used.[40] The reference WebAssembly emitter binaryen used to contain an asm2wasm
module, but it was removed after Emscripten stopped using it.[41]
See also
- CrossBridge
- RPython
- AssemblyScript
References
- ^ "asm.js in Firefox Nightly". Luke Wagner's blog. 21 Mar 2013. Retrieved 13 Nov 2014.
- ^ a b "kripken/emscripten · GitHub". Github.com. Retrieved 2015-03-05.
- ^ a b "Firefox 22.0 release notes". Mozilla. Retrieved July 4, 2013.
- ^ "Asm.js". Asm.js. Retrieved 2015-03-05.
- ^ "asm.js — frequently asked questions". Asmjs.org. July 26, 2014.
- ^ "asm.js". Asm.js. Retrieved 2015-03-05.
- ^ a b Alon Zakai; Robert Nyman (20 December 2013). "Gap between asm.js and native performance gets even narrower with float32 optimizations". Retrieved 11 April 2014.
- ^ "Bringing Asm.js to Chakra and Microsoft Edge". Microsoft. May 7, 2015. Retrieved May 7, 2015.
- ^ "Chrome 28 Beta: A more immersive web, everywhere". Google. Retrieved 2013-07-06.
- ^ "Home — Demos — Games and Game Engines".
- ^ "plu". Themucker.github.io. Archived from the original on 2013-08-03. Retrieved 2015-03-05.
- ^ "repl.it — Python". Repl.it. Retrieved 2015-03-05.
- ^ "repl.it — Ruby". Repl.it. Retrieved 2015-03-05.
- ^ "pepper.js Examples". Trypepperjs.appspot.com. Archived from the original on 2020-02-14. Retrieved 2015-03-05.
- ^ "emscripten-qt — Demos". Vps.etotheipiplusone.com. Archived from the original on 2015-02-13. Retrieved 2015-03-05.
- ^ "About Emscripten".
- ^ "Vim.js — JavaScript port of Vim". Coolwanglu.github.io. Retrieved 2015-03-05.
- ^ "TrueType Fonts in JavaScript". Archived from the original on 2012-10-12.
- ^ "Port of SQLite to Javascript". Github.com. Retrieved 2015-03-05.
- ^ "GnuPG.js". Manuuels.github.io. Retrieved 2015-03-05.
- ^ "ctags in the browser". Github.com. Retrieved 2015-03-05.
- ^ "Gnuplot online". Gnuplot.respawned.com. Archived from the original on 2015-02-22. Retrieved 2015-03-05.
- ^ "A hack to put GraphViz on the web". Github.com. Retrieved 2015-03-05.
- ^ "JavaScript port of ZLib DEFLATE for the browser". Github.com. Retrieved 2015-03-05.
- ^ "Epic Games Releases 'Epic Citadel' on the Web". UnrealEngine.com (Press release). May 2, 2013. Archived from the original on November 30, 2016. Retrieved October 24, 2014.
- ^ "Unreal Engine 3 ported to JavaScript and WebGL, works in any modern browser". ExtremeTech. Ziff Davis. Retrieved 2015-03-05.
- ^ "On the future of Web publishing in Unity". Blogs.unity3d.com. April 29, 2014.
- ^ "HTML5". Clb.demon.fi. Archived from the original on 2015-03-06. Retrieved 2015-03-05.
- ^ "Compiling for the Web". godotengine.org. November 10, 2016.
- ^ "Emscripten-Generated Code". Kripken.github.io. Retrieved 2015-03-05.
- ^ "Emscripten-Generated Code". Forandom.github.io. Retrieved 2015-03-05.
- ^ Guryanov Aleksander. "Dune 2 - Online (browser version)". Epicport. Retrieved 2015-03-05.
- ^ "Mozilla Banana Bread Demo". Developer.mozilla.org. Retrieved 2015-03-05.
- ^ "Humble Mozilla Bundle pushes WebGL-powered browser gaming". Ars Technica. 15 Oct 2014. Retrieved 15 Oct 2014.
- ^ "EM-Dosbox on Github". Retrieved 2015-04-09.
- ^ "Page Redirection". Jsmess.textfiles.com. Retrieved 2015-03-05.
- ^ "HTML5 Fractal Playground". Danielsadvernture.info. Archived from the original on 2015-02-22. Retrieved 2015-03-05.
- ^ "FAQ". WebAssembly.
- ^ "TC39 proposal for SIMD.js". Ecma TC39. 23 June 2020.
- ^ "WebAssembly/binaryen". GitHub. WebAssembly. 25 June 2020.
AssemblyScript which compiles TypeScript to Binaryen IR; wasm2js which compiles WebAssembly to JS
- ^ "Binaryen Changelog". GitHub.
v97: Remove asm2wasm, which supported Emscripten's fastcomp backend, after fastcomp was removed.
(See also the PR#3042.)