官术网_书友最值得收藏!

  • Learn WebAssembly
  • Mike Rourke
  • 328字
  • 2021-08-13 15:38:47

Mozilla and asm.js

Mozilla debuted asm.js in 2013 and provided a way for developers to translate their C and C++ source code to JavaScript. The official specification for asm.js defines it as a strict subset of JavaScript that can be used as a low-level, efficient target language for compilers. It's still valid JavaScript, but the language features are limited to those that are amenable to ahead-of-time (AOT) optimization. AOT is a technique that the browser's JavaScript engine uses to execute code more efficiently by compiling it down to native machine code. asm.js achieves these performance gains by having 100% type consistency and manual memory management.

Using a tool such as Emscripten, C/C++ code can be transpiled down to asm.js and easily distributed using the same means as normal JavaScript. Accessing the functions in an asm.js module requires linking, which involves calling its function to obtain an object with the module's exports.

asm.js is incredibly flexible, however, certain interactions with the module can cause a loss of performance. For example, if an asm.js module is given access to a custom JavaScript function that fails dynamic or static validation, the code can't take advantage of AOT and falls back to the interpreter:

The asm.js AOT compilation workflow

asm.js isn't just a stepping stone. It forms the basis for WebAssembly's Minimum Viable Product (MVP). The official WebAssembly site explicitly mentions asm.js in the section entitled WebAssembly High-Level Goals.

So why create WebAssembly when you could use asm.js? Aside from the potential performance loss, an asm.js module is a text file that must be transferred over the network before any compilation can take place. A WebAssembly module is in a binary format, which makes it much more efficient to transfer due to its smaller size. 

WebAssembly modules use a promise-based approach to instantiation, which takes advantage of modern JavaScript and eliminates the need for any is this loaded yet? code.

主站蜘蛛池模板: 疏附县| 奈曼旗| 墨脱县| 庆元县| 长乐市| 滦南县| 罗城| 稻城县| 诏安县| 武义县| 邮箱| 得荣县| 固阳县| 昂仁县| 安陆市| 庆安县| 石泉县| 绥中县| 梧州市| 壶关县| 多伦县| 武汉市| 宜兰市| 南华县| 长乐市| 崇仁县| 德江县| 昌黎县| 逊克县| 三明市| 兴化市| 嘉定区| 罗田县| 休宁县| 嘉兴市| 健康| 方正县| 铜鼓县| 兴安县| 江安县| 荆门市|