Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
instanceof Array vs Array.isArray()
(version: 0)
Comparing performance of:
instanceof Array vs Array.isArray()
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(1000).fill(null); for(let i = 0; i < arr.length; i++) { if(i % 3 === 0) { arr[i] = [1]; } else if(i % 2 === 0) { arr[i] = {a: 1}; } else { arr[i] = '1'; } }
Tests:
instanceof Array
let circle = 5; while(circle > 0) { const check = arr.map(item => item instanceof Array); circle--; }
Array.isArray()
let circle = 5; while(circle > 0) { const check = arr.map(item => Array.isArray(item)); circle--; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
instanceof Array
Array.isArray()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
instanceof Array
2815.7 Ops/sec
Array.isArray()
2878.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the benchmark and its results. **Benchmark Definition** The benchmark is designed to compare two approaches for checking if an object is an array in JavaScript: 1. Using the `instanceof` operator (`instanceof Array`) 2. Using the `Array.isArray()` method The benchmark creation code creates a large array of 1000 elements, fills it with different types of values (null, arrays, objects, and strings), and then uses a while loop to iterate over the array. **Options Compared** The two options being compared are: 1. **`instanceof Array`**: This operator checks if an object is a direct instance of the `Array` class. 2. **`Array.isArray()`**: This method returns a boolean value indicating whether the given value is an array or not. **Pros and Cons of Each Approach** * **`instanceof` operator**: + Pros: - Simple and efficient - Works well for checking if an object is a direct instance of `Array` + Cons: - Can be slower than using `Array.isArray()` because it involves a type check - May not work as expected if the object has inherited properties from `Array` * **`Array.isArray()` method**: + Pros: - Faster and more efficient than using `instanceof` - Handles cases where the object is an array-like value (e.g., a string that can be iterated over) + Cons: - May return incorrect results if the input is not a valid array - Less readable in some cases, as it's not immediately clear what the method does **Library** There is no explicit library mentioned in the benchmark definition. However, the `Array.isArray()` method is a built-in JavaScript method. **Special JS Feature or Syntax** The benchmark uses a feature called "array destructuring" (not explicitly used in this case, but implied by the use of `arr.map()`) and also uses modern JavaScript syntax such as arrow functions (`=>`), template literals (`\r\n`), and the `for...of` loop (not explicitly used in this case). **Other Considerations** The benchmark is designed to test the performance difference between these two approaches on a large array of mixed data types. The use of a while loop with a decreasing counter (`circle--`) creates a microbenchmark that can be executed multiple times, allowing for accurate measurements. **Alternatives** If you were to create a similar benchmark, you could consider using other methods or libraries to check if an object is an array, such as: * Using `typeof` operator to check the type of the object (`typeof obj === 'object' && Array.isArray(obj)`) * Using a library like Lodash's `isArray()` function * Creating a custom function to check if an object is an array (e.g., using regex or a more complex algorithm) Keep in mind that the best approach will depend on the specific use case and requirements of your project.
Related benchmarks:
Array isArray vs instanceof
Array isArray vs instanceof 2
instanceof Array vs Array.isArray
Alternate Array.isArray vs instanceof with nonsense
Alternate Array.isArray vs instanceof with extra nonsense
Comments
Confirm delete:
Do you really want to delete benchmark?