Vinay K
Vinay K

Reputation: 5572

Which one is better in performance in accessing length of array

I have an array. I would like do some operation based on the presence of items in the array.

Which approach is better in performance using javascript.

if (!myArr.length) {
   //do something.
}

OR

if (myArr.length === 0) {
   // do something
}

Upvotes: 0

Views: 24

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074178

Which approach is better in performance using javascript

The answer to this question for JavaScript code is almost always:

  1. It depends on the engine, and

  2. If it matters, test it on the engines you need to support. Here's a test you can use.

I very much doubt that it matters, though, this is unlikely to be a performance bottleneck in your code.

Upvotes: 2

Related Questions