简体   繁体   English

JavaScript:通过优化名称查找,性能会提高多少?

[英]JavaScript: What is the performance gain by optimizing Name Lookups?

The company I work at bought us (or rather me) the jQuery Cookbook by O'Reilly which I am reading from front to back. 我工作的公司给我们(或更确切地说是我)买了O'Reilly的jQuery Cookbook,我从头到尾都在阅读。 Now I am at 5.13 which talks about Name Lookups. 现在,我在5.13讨论名称查找。 It claims that given the right circumstances, code which is optimized regarding Name Lookups can have an up to 70% performance boost. 它声称,在适当的情况下,针对名称查找进行优化的代码可以将性能提高多达70%。 In one very situational example, it claims to be even 35x faster and cutting execution time from 7 seconds to a mere 0.2 seconds! 在一个非常有情境的示例中,它声称速度甚至快了35倍,并将执行时间从7秒减少到仅0.2秒!

My Question is: By which factor does a Name Lookup Optimization enhance performance in a real live situation an is it worth the extra few hours on a 4k JS-lines AJAX Application? 我的问题是:在实际情况下,名称查找优化可以通过哪个因素来提高性能?在4k JS行AJAX应用程序上花几个小时值得吗?

The factor which it helps depends on how it is used really. 它帮助的因素取决于它的实际使用方式。 Its like asking how long is a piece of string. 就像问一根弦多长时间。

I have seen setting names for true,false, undefined can make an application at least 10x faster in the javascript. 我已经看到将名称设置为true,false,undefined可以使应用程序在javascript中的速度至少快10倍。 The reason why it become faster is that it doesn't need to look inside the global scope chain. 之所以变得更快,是因为它不需要查看全局范围链。 Nick Zakas explains it quite well in the video below from google code 尼克·扎卡斯(Nick Zakas)在以下Google代码的视频中对此进行了很好的解释

http://googlecode.blogspot.com/2009/06/nicholas-c-zakas-speed-up-your.html http://googlecode.blogspot.com/2009/06/nicholas-c-zakas-speed-up-your.html

EDIT: 编辑:

I just came across this and thought it may be useful. 我刚遇到这个问题,并认为它可能有用。 its a quick explanation of the scoping chain too. 它也是作用域链的快速说明。 http://www.phpied.com/extreme-javascript-optimization/ http://www.phpied.com/extreme-javascript-optimization/

Nicholas Zakas did some researches on the subject and talks about this on this Google Tech Talk . 尼古拉斯·扎卡斯(Nicholas Zakas)对此主题进行了一些研究,并在此Google技术讲座中对此进行了讨论 It seems that you can get an interesting performance enhancement. 看来您可以获得有趣的性能增强。 I'm not sure about the exact number, but the sure thing is that optimizing name lookups will improve the speed of your code. 我不确定确切的数字,但是确定的事情是优化名称查找将提高代码的速度。

Of course if you're dragging 10 years of legacy code behind you that is working just fine, don't worry about optimizing... but if your application is slow, I'd consider optimizing the code. 当然,如果您要拖延10年的旧代码运行良好,就不必担心优化...但是,如果您的应用程序运行缓慢,我会考虑优化代码。

If you decide to go with refactoring, try to pinpoint what you can optimize. 如果您决定进行重构,请尝试指出可以优化的内容。 I recommend using the the javascript profiler of firebug to do so. 我建议使用firebugjavascript分析器来执行此操作。

You can even try optimizing a minor part of your code, see the performance boost that you get and then figure out if it's worth optimizing the rest of your app. 您甚至可以尝试优化代码的一小部分,查看所获得的性能提升,然后找出是否值得优化其余应用程序。

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

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