Monday, January 28, 2019

Most efficient way to check if a variable is an array in JavaScript?

Answer



I was reading this question about how to check if a variable is an Array in Javascript, and this answer proposes several solutions:



1. variable.constructor === Array
2. Array.isArray(variable)
3. variable instanceof Array



The post has several updates and they touch on the efficiency of the different solutions, but between the post and its comments it isn't entirely clear which solution is the most efficient. I'm hoping to clarify which of these solutions provides the most efficient check for whether or not a variable is an Array in JavaScript.



Edit: I'd like to note that this question is about the performance of checking whether or not a variable is an Array and not how to check if a variable is an Array. Considering all the noise in the linked question, I believe there is value to the question.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...