简体   繁体   English

当asm.js比普通JS代码更快时,我为什么要在JS中编写新代码?

[英]When asm.js is faster than normal JS code, why should I write new code in JS?

Emscripten could generate faster code from C/C++ than JS code wrote by hands, so is that means we should write new code in C/C++ and compile them to run on Web? Emscripten可以从C / C ++生成比用手写的JS代码更快的代码,这是否意味着我们应该用C / C ++编写新代码并编译它们以在Web上运行?

I read the Emscripten FAQ, it says "By all means write new JavaScript code.", why is that? 我阅读了Emscripten FAQ,它说“通过各种方式编写新的JavaScript代码。”,为什么会这样?

asm.js is not a faster way to execute Javascript-esque code. asm.js不是执行Javascript-esque代码的更快的方法。 It is a faster way to run code which is already reduced to the level of abstraction of machine code. 这是一种更快的运行代码的方法,已经降低到机器代码的抽象级别。 You seem to greatly overestimate the gains: 你似乎大大高估了收益:

  • If you let JS developers write C++ as if it's JS, you end up with fugly code that's not nearly as fast as C++ could be and flawed in other ways too. 如果你让JS开发人员编写C ++就好像它是JS一样,你最终会得到一些不像C ++那么快的代码,并且在其他方​​面也存在缺陷。
  • Many potential bottlenecks, such DOM manipulation and network latency, aren't affected at all by how fast your code is running. 许多潜在的瓶颈,例如DOM操作和网络延迟,完全不受代码运行速度的影响。
  • For many programs, the speed up from a faster language implementation is dwarfed by the speed up of high-level optimizations. 对于许多程序而言,从更快的语言实现加速相比,高级优化的速度相形见绌。 In other words, doing work somewhat faster is nice, but not doing it at all is even faster. 换句话说,更快地完成工作很不错,但是根本不这样做会更快。

Going this route has significant downsides as well: 走这条路也有很大的缺点:

  • You have to throw away your working code, and re-write it (including bugs) in a language most of your team won't know nearly as well, if at all. 你必须抛弃你的工作代码,然后用你所支持的大多数人都不会知道的语言重写它(包括错误),如果有的话。
  • As of now, the technology is still in its infancy. 截至目前,该技术仍处于起步阶段。 You wouldn't bet your company, or even an important product, on it. 你不会打赌你的公司,甚至是重要的产品。 Even if it succeeds, it will always be a niche technology compared to JavaScript. 即使它成功了,与JavaScript相比,它也将永远是一种利基技术。 This doesn't disqualify it for professional work, but it will make many things harder. 这不会是资格的专业工作,但它会使很多事情更难。
  • IIUC, you can't directly do most things JS can do beyond crunching numbers, you can only call into JS functions explicitly offered to the asm.js module. IIUC,你不能直接做大多数事情JS可以做的超越数字,你只能调用JS函数显式提供给asm.js模块。 That is, you'll always need at least a bunch of glue code in Javascript, and (as mentioned above) if this includes your bottlenecks, you didn't actually gain anything. 也就是说,你总是需要在Javascript中至少有一堆粘合代码,并且(如上所述)如果这包括你的瓶颈,你实际上并没有获得任何东西。

The only kinds of code which I expect to gain enough from asm.js to use are: 我期望从asm.js中获得足够多的代码是:

  • Existing code which isn't already written in JavaScript. 现有代码尚未用JavaScript编写。 For the sole reason of saving you most of the porting hassle. 唯一的原因是为您节省了大部分移植麻烦。
  • Heavy number crunching not interacting with the browser. 重数字运算没有与浏览器交互。 (Think: How often do you do this? And do you really want to go through the effort of writing it in C/C++ and wiring it up with JS?) (想一想:你经常这样做吗?你真的想要用C / C ++编写它并用JS连接吗?)
  • Stuff which is, by its nature, at the level of abstraction asm.js supports, such as compilers emitting machine code-esque instruction. 就其本质而言,在asm.js的抽象层面上支持的东西,例如发出机器代码 - esque指令的编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM