JavaScript 即时编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7807235/
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
JavaScript Just In Time compilation
提问by hari
I have a quite big JavaScript for HTML page for a device.
我有一个用于设备的 HTML 页面的相当大的 JavaScript。
But it's a bit slow. I tried compressing JavaScript files but it's still not satisfactory.
但它有点慢。我尝试压缩 JavaScript 文件,但仍然不令人满意。
So I was thinking, is it possible to make it as a Just in Timethat is compiled converted to machine code and use it? (Hope my understanding is correct) I use a WebKit based browser.
所以我在想,是否有可能将其作为即时编译为机器代码并使用它?(希望我的理解是正确的)我使用基于 WebKit 的浏览器。
Anybody please who have done this, please provide links to "How To" pages or info about the same.
请任何已经完成此操作的人,请提供指向“操作方法”页面或相关信息的链接。
回答by Spudley
Both Safari and Chrome do JIT compilation of Javascript already. In fact, the only browser in widespread use that doesn't is IE8 and earlier. This is one of the main reasons why IE8 is so much slower than the competition these days.
Safari 和 Chrome 都已经对 Javascript 进行了 JIT 编译。事实上,唯一没有广泛使用的浏览器是 IE8 及更早版本。这是当今 IE8 比竞争对手慢得多的主要原因之一。
But reading between the lines of your question, my guess is that you're not quite understanding what JIT compilation is. JIT compilation happens on the browser; you don't need to change your code in any way at all in order for the browser to be able to do JIT compilation on it for you.
但是仔细阅读您的问题,我的猜测是您不太了解 JIT 编译是什么。JIT 编译发生在浏览器上;您根本不需要以任何方式更改您的代码,以便浏览器能够为您进行 JIT 编译。
What it sounds like you're actually thinking of is bytecode compilation, such as Java does. This bytecode is effectively a half-way compiled language which is then itself JIT compiled when you run the program. If this is what you're thinking of, I can confirm that this is not an option for browser-based Javascript code.
听起来您实际上在考虑的是字节码编译,就像 Java 那样。该字节码实际上是一种中途编译语言,然后在您运行程序时对其进行 JIT 编译。如果这是您的想法,我可以确认这不是基于浏览器的 Javascript 代码的选项。
Google have been toying with a technology called 'Native Client' (NaCl), which would allow you to provide compiled code to the browser, but this is not available yet except in development versions of Chrome.
谷歌一直在玩弄一项名为“本地客户端”(NaCl)的技术,它允许您向浏览器提供编译代码,但除了 Chrome 的开发版本外,该技术尚不可用。
In any case, compiling may make your code run quicker, but it wouldn't solve the fundamental issue of why it's running slowly, which is likely to be a far better thing to resolve. (even compiled code will perform badly if it has bottlenecks; compilation in itself doesn't magically make slow code better)
无论如何,编译可能会使您的代码运行得更快,但它并不能解决为什么它运行缓慢的根本问题,这可能是一个更好的解决方案。(即使编译后的代码如果有瓶颈也会表现得很差;编译本身并不会神奇地使慢速代码变得更好)
If you want to find out why your script is running slowly, I recommend using a profiling tool, such as the one built into Firebug or Chrome's Developer Tools. This will help you identify the parts of your code which are running slowly.
如果您想找出脚本运行缓慢的原因,我建议您使用分析工具,例如内置于 Firebug 或 Chrome 开发人员工具中的工具。这将帮助您识别运行缓慢的代码部分。
You could also try the YSlowtool, which can also give useful information on javascript performance.
您也可以尝试使用YSlow工具,它也可以提供有关 javascript 性能的有用信息。
You also state that you've compressed your script to try to get it to go faster. Compressing the script will help it to downloadquicker (because it's a smaller file), but it won't do anything for the speed that the code runs at.
您还声明您已压缩脚本以尝试使其运行得更快。压缩脚本将有助于它更快地下载(因为它是一个较小的文件),但它不会对代码运行的速度产生任何影响。
I hope that helps.
我希望这有帮助。