1

http://jsperf.com/testing-for-empty-array

I was wondering what the best way to test for an empty array in JavaScript was and found some strange results. From what I can tell, you should never ever test using:

if (arr == true)
  // do stuff

which I thought was interpreted identical to:

if (arr)
  // do stuff

Is there a reason why using arr == true is significantly slower?

15
  • 1
    isn't arr.length !== 0 even faster? Commented Jul 7, 2013 at 3:33
  • 3
    Isn't arr == true always false? Whether empty or not? Commented Jul 7, 2013 at 3:34
  • But shouldn't it go through the same tests in the if (arr) case? Commented Jul 7, 2013 at 3:35
  • 1
    @RyanAllred - The first element of the array could be the value undefined. JavaScript doesn't have to traverse the array to determine its length. Note that if(arr) will always be true if arr is an array, empty or not. You need to test arr.length (and possibly use Array.isArray(arr)) to test whether arr is an empty array. Commented Jul 7, 2013 at 3:45
  • 1
    @RyanAllred Don't rely on the benchmark like this. jsperf makes sure the browser doesn't use their optimizing compiler in here, which in a case you need performance, you will be using optimizing compiler in your app.Then the performance results might be even different for storing different things in array(like small integers which can be optimized), or if the array is empty or not. The best is that just use !a.length or a.lengt`, then later if you profile and found out you need to optimize that bit, you can replace it with other things if it makes significant improvement in your application. Commented Jul 7, 2013 at 3:58

2 Answers 2

2

The only good way to test if an array is empty is with arr.length.

Let see different case.

  1. if(arr === true) will always be false since array is not a boolean.

    if(arr !== false && arr !== true) will return true for the same reason!

  2. if(arr) will return true or false, it depend if the array is set. If the var arr = [], it will return true, but it is an empty array.

    In fact, even if the var is not an array, it will return true.

  3. if(arr[0]) will return true if there is a value in the cell with the index 0. Even there, it doesnt mean it is empty, because you can set a cell like that var arr=[]; arr[1] = 'value' and arr[0] will return false because it is not set, but the array have a length of 2 (with one cell).

Finally, the best way to see if an array is empty is like that : if(arr && arr.length).

checking if arr exist not needed if you know that you have created the array, but else, it will prevent any errors. Then, you check the length of the array and if you haven't changed the value like that : arr.length = 1000, it will tell you if the array is empty.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually, console.log([1] == true) will print true. Remember that in JavaScript, == does automatic type conversion.
in #3, if you set arr[0], the length will be 2. Just saying.
The spec is pretty clear in that aspect. [1] == true => [1] == 1 => '1' == 1 => 1 == 1 => true
There's a complex series of type conversions going on. First, true is converted to a number (which will be 1). Then, the array is converted to a primitive, which will be the string '1' (a comma-separated list of the values in the array). Then the string is also converted to a number. If the numbers match, then the two values are considered equal. (Note that the left side may be NaN.)
^ Yes that is way more well explained than my comment. =]
2

It has to convert the array first to an empty string and from there into a boolean to compare against true, because you can't compare an array directly with a boolean. Casting is expensive.

6 Comments

why does it have to do that?
Why does it have to do what? Compare? Cast?
@chris - The rules for equality comparison, including what type conversions are required, is spelled out in section 11.9.3 of the EcmaScipt specification.
why cast to string? an object is always truthy, is it not?
Here's a direct link to #11.9.3. @chris step 8-9 calls ToPrimitive on the object on the second pass of the algorithm (first one casts the boolean to number). The ToPrimitive of an array is the string representation of its elements concatenated by ,.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.