Javascript asm.js 和 WebAssembly 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31502563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:46:13  来源:igfitidea点击:

What is the difference between asm.js and WebAssembly?

javascriptasm.jswebassembly

提问by NeoWang

I have been reading about asm.js and WebAssembly recently:

我最近一直在阅读有关 asm.js 和 WebAssembly 的文章:

http://ejohn.org/blog/asmjs-javascript-compile-target/

http://ejohn.org/blog/asmjs-javascript-compile-target/

https://brendaneich.com/2015/06/from-asm-js-to-webassembly/

https://brendaneich.com/2015/06/from-asm-js-to-webassembly/

I am still confused about a few things:

我仍然对一些事情感到困惑:

  1. Is asm.js code compiled in time and run? Compiled into what?
  2. Other than asm.js being text and wasm (web assembly) being binary, what are the differences between the 2?
  3. What does this mean for other scripting languages, running in the browser? Take python for example, is it going to be
    • python code compiled to wasm? or
    • python interpreter (Cpython) compiled into wasm and interpret python?
  1. asm.js 代码是否及时编译并运行?编译成什么?
  2. 除了 asm.js 是文本和 wasm(Web 程序集)是二进制之外,两者之间有什么区别?
  3. 这对于在浏览器中运行的其他脚本语言意味着什么?以python为例,会不会
    • python代码编译成wasm?或者
    • python解释器(Cpython)编译成wasm并解释python?

采纳答案by Bergi

Is asm.js code compiled in time and run? Compiled into what?

asm.js 代码是否及时编译并运行?编译成什么?

asm.js is regular javascript code, and is compiled into bytecode by the JS interpreter as always. However, an interpreter with asm support is supposed to do ahead-of-time compilation, and possibly to generate more efficient code representation because of the static typing. See http://asmjs.org/for details.

asm.js 是普通的 javascript 代码,和往常一样被 JS 解释器编译成字节码。但是,具有 asm 支持的解释器应该进行提前编译,并且可能由于静态类型而生成更有效的代码表示。有关详细信息,请参阅http://asmjs.org/

what are the differences between asm and wasm (other than text vs binary)?

asm 和 wasm 之间有什么区别(文本与二进制除外)?

None, for now. wasm is supposed to be backwards-compatible, compilable to asm(which again is executable as normal JS). It might however be extended with more featuresin the future as support for it grows.

没有,暂时没有。wasm 应该是向后兼容的,可以编译为 asm(它也可以像普通 JS 一样执行)。然而,随着对它的支持的增长,它可能会在未来扩展更多功能

What does this mean for other scripting languages, running in the browser?

这对于在浏览器中运行的其他脚本语言意味着什么?

The latter, rather, as Python still needs to be interpreted. Scripting languages that don't need an interpreter can of course be directly compiled to (w)asm, given that there is a compiler (chain) that supports it as a target.

相反,后者作为 Python 仍然需要解释。不需要解释器的脚本语言当然可以直接编译为 (w)asm,因为有一个编译器(链)支持它作为目标。

回答by cristian v

asm.jsis a subset of JS with "highly optimizable" instructions. Basically you can declare the type (int, float) and the js engine (in the browsers but also the node.js one) will execute the instructions faster. It has benefits if your app does a lot of calculation or graphic if used together with WebGL.

asm.js是具有“高度可优化”指令的 JS 子集。基本上你可以声明类型 (int, float) 和 js 引擎(在浏览器和 node.js 中)将更快地执行指令。如果您的应用程序与 WebGL 一起使用,它会进行大量计算或图形处理,这将带来好处。

web assemblyis a binary format for JS, all JS, not only asm.js. It's not a bytecode, it's a binary encoding of the AST that the parser calculates. It has 2 big benefits:

web程序集是JS的二进制格式,所有的JS,不仅仅是asm.js。它不是字节码,而是解析器计算的 AST 的二进制编码。它有两大好处:

  • the JS engine can skip the parsing step
  • it's much more compact than the JS original source
  • JS引擎可以跳过解析步骤
  • 它比 JS 原始源代码紧凑得多

We already can write code for browsers that isn't JS: EMSCripten can compile c++ code in JS code. Other transcompiler are already available to compile your code into JS. Using asm.js that code can run faster when it does math. Using web assembly that code will be more compact and the browser will be able to process it faster (because it will be able to skip the parsing). You won't have a new plugin to load like DirectX, JavaApplets, Flash or Silverlight because everything will run in the JS sandbox.

我们已经可以为非 JS 的浏览器编写代码:EMSCripten 可以在 JS 代码中编译 c++ 代码。其他转编译器已经可以将您的代码编译成 JS。使用 asm.js,代码在进行数学运算时可以运行得更快。使用 web 程序集,代码将更紧凑,浏览器将能够更快地处理它(因为它将能够跳过解析)。您不会像 DirectX、JavaApplets、Flash 或 Silverlight 那样加载新插件,因为一切都将在 JS 沙箱中运行。

回答by Luke

Is asm.js code compiled in time and run? Compiled into what?

asm.js 代码是否及时编译并运行?编译成什么?

Different browsers compile asm.js code in different ways. As of August 2015:

不同的浏览器以不同的方式编译 asm.js 代码。截至 2015 年 8 月:

  • Firefox compiles asm.js to machine code (and caches the machine code for future loads of the same asm.js) [1].
  • In Windows 10 as an experimental flag, Edge will also do some Ahead-of-Time validation and compilation of asm.js [2].
  • Chrome specially recognizes the "use asm" directive at the beginning of asm.js to parse and analyze it the code more eagerly and tweak compilation heuristics.
  • Safari does no special processing of asm.js.
  • Firefox 将 asm.js 编译为机器代码(并缓存机器代码以备将来加载相同的 asm.js)[ 1]。
  • 在 Windows 10 作为实验标志,Edge 还将对 asm.js [ 2]进行一些 Ahead-of-Time 验证和编译。
  • Chrome 特别识别了 asm.js 开头的“use asm”指令,以便更积极地解析和分析代码并调整编译启发式。
  • Safari 不会对 asm.js 进行特殊处理。

Other than asm.js being text and wasm (web assembly) being binary, what are the differences between the 2?

除了 asm.js 是文本和 wasm(Web 程序集)是二进制之外,两者之间有什么区别?

asm.js is just JavaScript and thus must behave exactly according to the JavaScript spec. As a new standard, WebAssembly is able to fix some corner cases where JavaScript behavior is not the ideal (from a performance or compilation perspective) [3]. In the future [4], WebAssembly will be able to add features that would otherwise be difficult to express in JavaScript.

asm.js 只是 JavaScript,因此必须完全按照 JavaScript 规范运行。作为一个新标准,WebAssembly 能够修复一些 JavaScript 行为不理想的极端情况(从性能或编译的角度来看)[ 3]。在未来 [ 4],WebAssembly 将能够添加原本难以用 JavaScript 表达的功能。

What does this mean for other scripting languages, running in the browser? Take python for example, is it going to be

  • python code compiled to wasm? or
  • python interpreter (Cpython) compiled into wasm and interpret python?

这对于在浏览器中运行的其他脚本语言意味着什么?以python为例,会不会

  • python代码编译成wasm?或者
  • python解释器(Cpython)编译成wasm并解释python?

In v.1, the simplest way to run Python in a browser will be to compile a Python interpreter to wasm, as you said. This means, e.g., the Python GC is running in wasm code and manually managing the wasm linear memory. There has already been an experimental projects to add an asm.js backend to PyPy [5] (which could work just as well for wasm). It currently runs into limitations of asm.js that could be addressed by the dynamic linking future featureof wasm. Going further, wasm seeks to provide both GC integrationand JIT compilation supportboth of which would allow more efficient and natural integration with the Web platform.

在 v.1 中,如您所说,在浏览器中运行 Python 的最简单方法是将 Python 解释器编译为 wasm。这意味着,例如,Python GC 在 wasm 代码中运行并手动管理 wasm 线性内存。已经有一个实验项目将 asm.js 后端添加到 PyPy [ 5](这对 wasm 也能正常工作)。它目前遇到了 asm.js 的局限性,可以通过wasm的动态链接未来特性来解决。更进一步,wasm 寻求提供GC 集成JIT 编译支持,这两者都将允许与 Web 平台更有效和自然的集成。