9

Which way is more efficient? Is there a difference?

This one:

var str = 'abc';

if(str.length == 20) {
    //...
}

if(str.length == 25) {
    //...
}

// and so on

Or this one:

var str = 'abc';
var length = str.length;

if(length == 20) {
    //...
}

if(length == 25) {
    //...
}

// and so on
Silver Light
  • 44,202
  • 36
  • 123
  • 164

4 Answers4

10

In the browsers where this might actually matter (read: IE) it will be calculated every time, so it's faster to store the value in a local variable.

http://jsperf.com/string-length


It used to be that

var len = someArray.length;
for (var i=0; i<len; i++) {
    // ...
}

was faster than

for (var i=0; i<someArray.length; i++) {
    // ...
}

but these days, V8's (Chrome's JS engine) optimizes the latter to run faster than the former. That's great - just remember, you don't really need to worry about performance in Chrome.


If you're curious to learn more about JavaScript performance, High Performance JavaScript is a solid read. Take its recommendations with a grain of salt, though, since a trick that makes code run faster in IE (6, 7, 8 or even 9) might very well make the code run slower in Chrome or Firefox 4.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

The second is by far the safer way to go. In the first you are assuming that it won't get recalculated. In the second you know that it won't. The second isn't always the best way though. It will only work when you know other processes won't affect the length of the array. So with global variables etc. you have to be careful. This can also apply to modifying the contents (length) of an array inside a for loop which stops at the upper bound of the array.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
1

Strings are immutable in JavaScript, so it is unlikely that even bad implementations of Javascript would recalcuate the length property of the string every time you access it.

You can actually test this yourself using jsperf; using Chrome 12, it actually looks like your first example is faster.

Community
  • 1
  • 1
Dexter
  • 18,213
  • 4
  • 44
  • 54
  • 1
    It's not a matter of _calculating_ the length - since strings are immutable, their length is a (constant) property. It's a matter of accessing that property repeatedly. – Matt Ball May 24 '11 at 15:00
  • @Matt - indeed, I was thinking in terms of C# properties which can hide complex calculations. My point was basically that because strings are immutable the length is a constant value, so there's unlikely to be significant performance differences between the OP's examples. (And as you've found, the snippets actually perform differently in different JS implementations) – Dexter May 24 '11 at 15:34
0

In theory, I would expect that the second code block should be quicker.

However, given that today's JS interpreters are actually highly optimised JIT compilers, I would imagine that they would spot that kind of thing and optimise it.

That should apply to pretty much all browsers in current mainstream use, with the obvious exception of IE8 and lower, where it's anyone's guess how it does it, but it'll be slow either way.

Spudley
  • 166,037
  • 39
  • 233
  • 307