var toStr = Object.prototype.toString
function toStrCase(obj){
return toStr.call(obj) === '[object Array]'
}
function consCase(obj){
return obj && obj.constructor === Array
}
function instCase(obj){
return obj && obj instanceof Array
}
var isArray = Array.isArray
isArray([])
instCase([])
consCase([])
toStrCase([])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.isArray | |
instanceof | |
[].constructor | |
Object.prototype.toString |
Test name | Executions per second |
---|---|
Array.isArray | 483002528.0 Ops/sec |
instanceof | 506267712.0 Ops/sec |
[].constructor | 113814080.0 Ops/sec |
Object.prototype.toString | 105179344.0 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and other considerations.
Benchmark Overview
The isArray
benchmark compares four different approaches to check if an object is an array:
Array.isArray
Object.prototype.toString
using a custom toStrCase
function[] .constructor
instanceof Array
These approaches are compared in various test cases, including testing each approach on an empty array ([]
).
Approaches Compared
Here's a brief explanation of each approach:
Array.isArray
: This is a built-in JavaScript method that returns a boolean indicating whether the object passed to it is an array.Object.prototype.toString
with toStrCase
function: The Object.prototype.toString
method returns a string representation of the object, which can be compared to the string '[object Array]'
. The custom toStrCase
function checks if this string matches the expected output. This approach is more verbose but allows for custom comparison logic.[] .constructor
: This approach uses the constructor
property of an empty array ([]
) to check if it's an array. Since arrays have a specific constructor, this method can be used to detect array-like objects.instanceof Array
: This is another built-in JavaScript method that returns a boolean indicating whether the object passed to it is an instance of Array
. However, since []
is not an instance of Array
, this approach will return false.Pros and Cons
Here are some pros and cons for each approach:
Array.isArray
: Pros - simple and concise; Cons - may be slower due to the additional method call.Object.prototype.toString
with toStrCase
function: Pros - allows for custom comparison logic; Cons - more verbose, potentially slower due to the custom function call.[] .constructor
: Pros - simple and efficient; Cons - may not work correctly for non-array-like objects.instanceof Array
: Pros - concise; Cons - will return false for []
, making it less suitable.Library Used
The toStrCase
function uses the Object.prototype.toString
method, which is a built-in JavaScript method.
Special JS Feature or Syntax
None of the approaches use any special JavaScript features or syntax. They are all standard JavaScript methods and properties.
Other Alternatives
Some alternative approaches to check if an object is an array include:
typeof
operator with 'array'
: typeof obj === 'array'
/^[\s\S]*$/.test(obj)
(this approach is not as efficient as the others)isArray
functionThese alternatives may have similar pros and cons to the approaches compared in the benchmark, but they are not part of the original implementation.