开发者

calling 5 times foo.length() more efficient or setting it to a variable and then calling it more efficient

开发者 https://www.devze.com 2023-03-28 02:54 出处:网络
I am new to javascript and I have a situation like where I check like if(foo.length>0){ // do something...

I am new to javascript and I have a situation like where I check like

if(foo.length>0){
 // do something...
}

I do it in 8 places. I wanted to know if its equal to i set foo.length() condition to a variable and then use that variable in if statement. Which one is efficient and how can I do it..?

Is开发者_StackOverflow there any way to perform this kind of task in javascript which is more efficient like in java we have apache commons stringUtils which is efficient, easy to use.


I would store it in a variable just for better readability, and a slightly smaller footprint. Now as a bonus you get imperceptibly better performance - and nobody can accuse you of unnecessary micro-optimization when you throw those arguments at them :) (Yes, I probably need help).


The performance gain is negligible. The string object already has the information about the length of the string, so using the length property doesn't do something like looping through all the characters in the string to find the end of it.

There is actually a drawback with putting the length of the string in a variable. You get two variables that are loosly coupled, and changing the string requires you to also update the length variable for the code to work properly. If you miss to update the length variable somewhere you will have inconsistent values.

Also, keeping the length variable updated can reduce the performance rather than increase it. You have to update the length variable every time the string changes, even if you don't use the length in between. You may end up reading the length properties more often than you would if you didn't have the variable.

Edit:

An actual performance test shows that the performance differs between browsers, but using the length property is generally faster than using a variable:

http://jsperf.com/length-in-a-variable


You can surly save the length of an array in a variable, and it performance better in some case. Especially if foo is not an real array but an list of DOM elements like you get it when calling document.getElementsByTagname().


If you need the value of foo.length in 8 different places, then yes, put it in a variable. But you're doing this to prevent redundancy foremost, not so much for efficiency.

Btw, the length property of array and array-like objects is not computed on retrieval. It is populated with a Number value at all times. The JavaScript engine updates its value automatically. (For instance, when you add a new array element to the array.) So, foo.length is an ordinary property retrieval - no methods or algorithms are executed internally on the array object.


Perhaps a function will help?

// returns true if the specified len is greater than the obj.length
// returns false of the object doesn't have a length property or if 
// the len is <= the obj.length
function checkLen(obj, len) {
    try {
        if(obj.length > len) return true;
    } catch(e) {
        console.log(e);
    }
    return false;
}
0

精彩评论

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