-4

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.

Bash
  • 628
  • 6
  • 19
  • 1
    The answer states that `variable.constructor === Array` is *the fastest method on Chrome, and most likely all other browsers*. So there you have your answer. – MauriceNino Jul 17 '19 at 14:14
  • 2
    There is a benchmark in one of the answers: [benchmark](http://jsben.ch/QgYAV). – zero298 Jul 17 '19 at 14:14
  • As with all performance questions, it depends. The best way to find out what is the most efficient in the circumstances you find yourself in, is to measure the performance, in the circumstances you find yourself in. – Heretic Monkey Jul 17 '19 at 14:14
  • @MauriceNino The benchmark says `isArray` is the fastest. – zero298 Jul 17 '19 at 14:14
  • There's a built-in which is Array.isArray. Why do you imagine you could replace it with a function faster but with same specs ? – Denys Séguret Jul 17 '19 at 14:16
  • I understand that you linked to the question I just proposed as a duplicate so you may think that it is not a duplicate, but the accepted answer has a benchmark that should help you see which one is optimal. – zero298 Jul 17 '19 at 14:16
  • @zero298 I did not write the answer, so yeah. Also, I don't know how this benchmark site works, but it seems like a pretty small sample data to actually determine the best one. – MauriceNino Jul 17 '19 at 14:16
  • This question aims to ask a different question than the previous question and it's clear the previous question's activity doesn't clarify this answer, because even in these comments the answers are varied. – Bash Jul 17 '19 at 14:17
  • 2
    They answer your question very clearly: There is no clear answer. The tests show them to be similar in performance but finally environment depended. If this very check is your bottleneck (which I doubt) than you will have to do proper tests yourself like people mentioned. Otherwise readability and use case matter more IMHO. – claasic Jul 17 '19 at 14:24
  • 1
    All three do different things? Take the one thats best for your usecase? Performance doesnt matter? Always relevant: https://ericlippert.com/2012/12/17/performance-rant/amp/ – Jonas Wilms Jul 17 '19 at 14:26
  • 2
    A question marked as a duplicate still has value. It serves as a signpost for others seeking the same information. The fact of the matter is that performance is context-dependent and measurement is almost always necessary to determine what the "fastest" method will be. The answers of both duplicates contain links to benchmarks evaluating the differences in raw performance in the context of the respective benchmark platforms. If that's your only requirement, then your question has been answered by those answers. If you want to know what's actually fastest in the context of your app, measure. – Heretic Monkey Jul 17 '19 at 14:36
  • @assoron I did not get that impression reading the answers. The impression I got was that the answer has evolved over time with preferred solutions changing since the question was asked. Understanding what the current fastest solution is and documenting it here was the point of this question. – Bash Jul 17 '19 at 14:41

1 Answers1

1

Thanks to the comments I received, I realized I missed a link to a benchmark, which I then edited to determine that variable instanceof Array is the most efficient of the solutions I was asking about in my question when using Chrome 74. Here is the benchmark.

Bash
  • 628
  • 6
  • 19