JavaScript 即时编译
我有一个用于设备的相当大的 JavaScript for HTML 页面。
但有点慢。我尝试压缩 JavaScript 文件,但仍然不令人满意。
所以我在想,是否有可能将其作为Just in Time编译转换为机器代码并使用它? (希望我的理解是正确的)我使用基于 WebKit 的浏览器。
任何做过此操作的人,请提供“操作方法”页面的链接或相关信息。
I have a quite big JavaScript for HTML page for a device.
But it's a bit slow. I tried compressing JavaScript files but it's still not satisfactory.
So I was thinking, is it possible to make it as a Just in Time that is compiled converted to machine code and use it? (Hope my understanding is correct) I use a WebKit based browser.
Anybody please who have done this, please provide links to "How To" pages or info about the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Safari 和 Chrome 都已经对 Javascript 进行了 JIT 编译。事实上,唯一不存在这种情况的广泛使用的浏览器是 IE8 及更早版本。这是当今 IE8 比竞争对手慢得多的主要原因之一。
但是阅读你问题的字里行间,我的猜测是你不太了解 JIT 编译是什么。 JIT 编译发生在浏览器上;您根本不需要以任何方式更改代码,浏览器就能够为您进行 JIT 编译。
听起来您实际上想到的是字节码编译,例如 Java 所做的。该字节码实际上是一种半路编译语言,然后在运行程序时对其本身进行 JIT 编译。如果这就是您的想法,我可以确认这不是基于浏览器的 Javascript 代码的选项。
谷歌一直在研究一种名为“Native Client”(NaCl)的技术,该技术允许您向浏览器提供编译后的代码,但除了 Chrome 的开发版本之外,该技术尚不可用。
无论如何,编译可能会让你的代码运行得更快,但它并不能解决运行缓慢的根本问题,这可能是一个更好解决的问题。 (即使是编译后的代码,如果存在瓶颈,也会表现得很差;编译本身并不会神奇地使缓慢的代码变得更好)
如果您想找出脚本运行缓慢的原因,我建议使用分析工具,例如内置的分析工具Firebug 或 Chrome 的开发者工具。这将帮助您识别代码中运行缓慢的部分。
您还可以尝试 YSlow 工具,它也可以提供有关 JavaScript 性能的有用信息。
您还声明您已经压缩了脚本以使其运行得更快。压缩脚本将有助于更快地下载(因为它是一个较小的文件),但它不会对代码运行的速度产生任何影响。
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.
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.
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.
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.
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.
You could also try the YSlow tool, which can also give useful information on javascript performance.
You also state that you've compressed your script to try to get it to go faster. Compressing the script will help it to download quicker (because it's a smaller file), but it won't do anything for the speed that the code runs at.