English 中文(简体)
能否将选定的/产生的内容储存在可变的业绩上?
原标题:Does storing selected/found elements in variable increase performance?

我制造了一个 j子,以改变我的航行。 遗憾的是,我必须接触并修改几乎所有的儿童内容,例如<条码>、>、ul、gt;、<条码>和>、li><a>等。 然后,这些要素需要一至四次。

我是否应当将其储存在变数中,或者如果需要,我是否应当查阅这些变量,例如$(my-nav )。

It just seems like a waste of memory to have 5 variables for maybe 25 lines of code. But I don t know if this is a acceptable trade-off for more performance.

我创建了一个Fiddle,以说明意思是什么:

最佳回答

Always good practice to cache your nodes. You can also benchmark yourself using http://jsperf.com/

You don t really need to worry about how much storage space variables need unless you are storing a massive DOM tree or something. Much more relevant the amount of work the JS engine has to do to locate the nodes.

EDIT
or even better, you can find an existing test case someone else has already created
http://jsperf.com/ns-jq-cached/4

问题回答

酌情扣押你的人力要素,无论你是否再次使用宝石,这无疑是明智的。

但我想到这个问题或许应该有帮助。 我对周围进行了搜索,找不到。

因此,我写了一封信真是迅速的。 http://blog.bucketsoft.com/05/lazy-loading-jquery-selector.html

(function($){
    var cachedObjects = new Array();

    $.lazy = function(selector) {
        if (typeof selector != "string" || arguments.length > 1) 
            return $.apply(this, arguments);

        var o = cachedObjects[selector];
        if (o == undefined)
        {
            o = $(selector);
            cachedObjects[selector] = o;
        }

        return o;
    };
})(jQuery);

You d use it like this...

$.lazy( .my-nav ).show();

Let me know if I ve overlooked anything. But I believe this would be good to use whenever the elements you re selecting are static and are never added or removed dynamically.

<>>>>>

I ve changed the code to make this more efficient. And I ve added a line to return $(selector) when the selector is not a string. So the caching will only work when the selector is a string.

<>12>>

现在,如果你不简单地通过护法,每 j客建议,就会打上<条码>。

Within the same function, I will cache the results of a DOM search in a local variable so I never have to do the same DOM search more than once in the same function. For most functions this will not be needed, but it s easy and safe to put the result in a local variable only for the duration of the function so I think it s a good habit.

我通常会把NOTCache DOM的电线输入全球变量,因为我试图避免全球变数,而如果某个特定功能需要,很少是一个实事求是的问题来检索OM的电线。 避免在全球变数中出现多功能参考的另一个原因是,如果某个特定的多功能点子被从多功能系统移走,如果你打算收集垃圾,如果在全球变数中提到了垃圾,那么多功能线就不会被收集垃圾,并可能导致记忆的泄露。

在一些情况下,我一再研究同样的DOM节点(如一个时间点),我将把DOM的节点推到职能关闭中的非全球变量,然后在当地使用这一变量。 但是,我发现需要很少这样做的情况。





相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...