可以将常规JavaScript转换为asm.js,还是只是为了加速静态类型的低级语言? [英] Can regular JavaScript be converted to asm.js, or is it only to speed up statically-typed low-level languages?

查看:455
本文介绍了可以将常规JavaScript转换为asm.js,还是只是为了加速静态类型的低级语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读如何使用asm进行测试和开发的问题。 js?,接受的答案提供了 http://kripken.github.com的链接/ mloc_emscripten_talk /#/

I have read the question How to test and develop with asm.js?, and the accepted answer gives a link to http://kripken.github.com/mloc_emscripten_talk/#/.

该幻灯片的结论是静态类型语言,特别是C / C ++可以有效地编译到JavaScript ,所以我们可以期望编译的C / C ++的速度比本机代码快2倍,或者更好,今年晚些时候

The conclusion of that slide show is that "Statically-typed languages and especially C/C++ can be compiled effectively to JavaScript", so we can "expect the speed of compiled C/C++ to get to just 2X slower than native code, or better, later this year".

但是非静态类型的语言呢,比如普通的JavaScript本身呢?是否可以编译为asm.js?

But what about non-statically-typed languages, such as regular JavaScript itself? Can it be compiled to asm.js?

推荐答案


JavaScript本身是否可以编译为asm.js ?

Can JavaScript itself be compiled to asm.js?

不是真的,因为它具有动态特性。这与尝试将其编译成至C 甚至到本机代码 - 您实际上需要随身携带一个VM来处理那些非静态方面。至少,这样的VM是可能的:

Not really, because of its dynamic nature. It's the same problem as when trying to compile it to C or even to native code - you actually would need to ship a VM with it to take care of those non-static aspects. At least, such a VM is possible:



js.js 是JavaScript中的JavaScript解释器。而不是尝试从头开始创建解释器, SpiderMonkey 编译成 LLVM 然后 emscripten 翻译输出到JavaScript。

js.js is a JavaScript interpreter in JavaScript. Instead of trying to create an interpreter from scratch, SpiderMonkey is compiled into LLVM and then emscripten translates the output into JavaScript.







但是如果asmjs代码运行速度比普通JS快,那么将JS编译为asmjs是否合理,不是吗?

But if asmjs code runs faster than regular JS, then it makes sense to compile JS to asmjs, no?

否。 asm.js 是一个非常有限的JS子集,可以很容易地转换为字节码。然而,您首先需要将JS的所有高级功能分解为该子集以获得此优势 - 这是一项非常复杂的任务。但JavaScript引擎经过精心设计和优化,可将直接的所有高级功能转换为字节码 - 所以为什么要打扰像asm.js这样的中间步骤呢? Js.js声称比原生JS慢大约200倍。

No. asm.js is a quite restricted subset of JS that can be easily translated to bytecode. Yet you first would need to break down all the advanced features of JS to that subset for getting this advantage - a quite complicated task imo. But JavaScript engines are designed and optimized to translate all those advanced features directly into bytecode - so why bother about an intermediate step like asm.js? Js.js claims to be around 200 times slower than "native" JS.


那么一般来说非静态类型的语言呢?

And what about non-statically-typed languages in general?

幻灯片从 ...只是C / C ++? 以后。具体来说:

The slideshow talks about that from …Just C/C++? onwards. Specifically:



动态语言

可以编译整个C / C ++运行时,并使用适当的语义解释原始语言
,但这不是轻量级的

Entire C/C++ runtimes can be compiled and the original language interpreted with proper semantics, but this is not lightweight

从这些语言到JavaScript的源到源编译器忽略
语义差异(例如,数字类型)

Source-to-source compilers from such languages to JavaScript ignore semantic differences (for example, numeric types)

实际上,这些语言依赖于特殊的VM来提高效率

Actually, these languages depend on special VMs to be efficient

他们的源到源编译器在优化上失败了在那些虚拟机中完成

Source-to-source compilers for them lose out on the optimizations done in those VMs


这篇关于可以将常规JavaScript转换为asm.js,还是只是为了加速静态类型的低级语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆