开发者

Why is caching values in objects taking more time?

开发者 https://www.devze.com 2023-03-01 13:54 出处:网络
As I have learnt, its better to cache the values in objects which we need repeatedly. For example, doing

As I have learnt, its better to cache the values in objects which we need repeatedly. For example, doing

var currentObj = myobject.myinnerobj.innermostobj[i]

and using 'currentObj' for further operations is better for performan开发者_开发技巧ce than just

myobject.myinnerobj.innermostobj[i]

everywhere, like say in loops.. I am told it saves the script from looking-up inside the objects every time..

I have around 1000 lines of code, the only change I did to it with the intention of improving performance is this (at many locations) and the total time taken to execute it increased from 190ms to 230ms. Both times were checked using firebug 1.7 on Firefox 4.

Is what I learnt true (meaning either I am overusing it or mis-implemented it)? Or are there any other aspects to it that I am unaware of..?


There is an initial cost for creating the variable, so you have to use the variable a few times (depending on the complexity of the lookup, and many other things) before you see any performance gain.

Also, how Javascript is executed has changed quite a bit in only a few years. Nowadays most browsers compile the code in some form, which changes what's performant and what's not. It's likely that the perforance gain from caching reference is less now than when the advice was written.


The example you have given appears to simply be Javascript, not jQuery. Since you are using direct object property references and array indices to existing Javascript objects, there is no lookup involved. So in your case, adding var currentObj... could potentially increase overhead by the small amount needed to instantiate currentObj. Though this is likely very minor, and not uncommon for convenience and readability in code, in a long loop, you could possibly see the difference when timing it.

The caching you are probably thinking of has to do with jQuery objects, e.g.

var currentObj = $('some_selector');

Running a jQuery selector involves a significant amount of processing because it must look through the entire DOM (or some subset of it) to resolve the selector. So doing this, versus running the selector each time you refer to something, can indeed save a lot of overhead. But that's not what you're doing in your example.

See this fiddle:

http://jsfiddle.net/SGqGu/7/

In firefox and chrome (didn't test IE) -- the time is identical in pretty much any scenario.


Is what I learnt true (meaning either I am overusing it or mis-implemented it)? Or are there any other aspects to it that I am unaware of..?

It's not obvious if either is the case because you didn't post a link to your code.

I think most of your confusion comes from the fact that JavaScript developers are mainly concerned with caching DOM objects. DOM object lookups are substantially more expensive than looking up something like myobj.something.something2. I'd hazard a guess that most of what you've been reading about the importance of caching are examples like this (since you mentioned jQuery):

var myButton = $('#my_button');

In such cases, caching the DOM references can pay dividends in speed on pages with a complex DOM. With your example, it'd probably just reduce the readability of the code by making you have to remember that currentObj is just an alias to another object. In a loop, that'd make sense, but elsewhere, it wouldn't be worth having another variable to remember.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号