并行 JavaScript 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3897625/
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
Parallel JavaScript Code
提问by Josh K
Is it possible to run JavaScript code in parallel in the browser? I'm willing to sacrifice some browser support (IE, Opera, anything else) to gain some edge here.
是否可以在浏览器中并行运行 JavaScript 代码?我愿意牺牲一些浏览器支持(IE、Opera、其他任何东西)来获得一些优势。
回答by JKirchartz
If you don't have to manipulate the dom, you could use webworkers ... there's a few other restrictions but check it out @ http://ejohn.org/blog/web-workers/
如果您不必操作 dom,则可以使用 webworkers ...还有一些其他限制,但请查看@ http://ejohn.org/blog/web-workers/
回答by Adam
Parallel.jshas a nice API for multithreaded processing in Javascript. It runs both in web browsers and in node.
Parallel.js有一个很好的 API,用于在 Javascript 中进行多线程处理。它在 Web 浏览器和 node.js 中运行。
回答by Ira Baxter
Perhaps it would be better to recode your JavaScript in something that generally runs faster, rather than trying to speed up the Javascript by going parallel. (I expect you'll find the cost of forking parallel JavaScript activities is pretty high, too, and that may well wipe out any possible parallel gain; this is common problem with parallel programming).
也许最好用通常运行得更快的东西重新编码你的 JavaScript,而不是试图通过并行来加速 Javascript。(我希望您会发现分叉并行 JavaScript 活动的成本也相当高,这很可能会消除任何可能的并行收益;这是并行编程的常见问题)。
Javascript is interpreted in most browsers IIRC, and it is dynamic on top of it which means it, well, runs slowly.
Javascript 在大多数浏览器 IIRC 中被解释,并且在它之上是动态的,这意味着它运行缓慢。
I'm under the impression you can write Java code and run it under browser plugins. Java is type safe and JIT compiles to machine code. I'd expect that any big computation done in Javascript would run a lot faster in Java. I'm not specifically suggesting Java; any compiled language for which you can get a plug in would do.
我的印象是您可以编写 Java 代码并在浏览器插件下运行它。Java 是类型安全的,JIT 编译成机器码。我希望在 Javascript 中完成的任何大型计算在 Java 中运行得更快。我并不是特别推荐 Java;您可以获得插件的任何编译语言都可以。
As an alternative, Google provides Closure, a JavaScript compiler. It is claimedto be a compiler, but looks like an optimizerto me and I don't know much it "optimizes". But, perhaps you can use that. I'd expect the Closure compiler to be built into Chrome (but I don't know for a fact) and maybe just running Chrome would get your JavaScript compiler "for free".
作为替代方案,Google 提供了Closure,一种 JavaScript 编译器。它声称是一个编译器,但对我来说看起来像一个优化器,我对它“优化”知之甚少。但是,也许你可以使用它。我希望 Closure 编译器内置到 Chrome 中(但我不知道事实),也许只是运行 Chrome 会“免费”获得您的 JavaScript 编译器。
EDIT: After reading about what about Closure does, as compiler guy I'm not much impressed. It looks like much of the emphasis is on reducing code size which minimizes download time but not necessarily performance. The one good thing they do in function inlining. I doubt that will help as much as switching to a truly compiled langauge.
编辑:在阅读了 Closure 的作用之后,作为编译器的我并没有给我留下多少印象。看起来大部分重点是减少代码大小,从而最大限度地减少下载时间,但不一定是性能。他们在函数内联中做的一件好事。我怀疑这与切换到真正编译的语言一样有帮助。
EDIT2: Apparantly the "Closure" compiler is different than the engine than runs JavaScript in Chrome. I'm told, but don't know this for a fact, that the Chrome engine has a real compiler.
EDIT2:显然“Closure”编译器不同于在 Chrome 中运行 JavaScript 的引擎。有人告诉我,但我不知道这一事实,Chrome 引擎有一个真正的编译器。
回答by Ira Baxter
Intel is coming up with an open-source project codenamed River Trailcheck out http://www.theregister.co.uk/2011/09/17/intel_parallel_javascript/
英特尔正在提出一个代号为River Trailcheck的开源项目http://www.theregister.co.uk/2011/09/17/intel_parallel_javascript/

