Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
instanceof Array vs Array.isArray() 2
(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 = 50; while(circle > 0) { const check = arr.map(item => item instanceof Array); circle--; }
Array.isArray()
let circle = 50; 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is testing two approaches to check if an object is an array: `instanceof Array` and `Array.isArray()`. The test creates a large array of 1000 elements, where every third element is an array, every second element is an object, and the rest are strings. The test then uses a while loop to iterate over the array, checking each element with both approaches. **What's being tested** The benchmark is testing which approach is faster: 1. `instanceof Array`: This checks if the object is an instance of the `Array` constructor function. 2. `Array.isArray()`: This checks if the object is an array using a separate method provided by the `Array` prototype. **Options compared** There are two options being compared: `instanceof Array` and `Array.isArray()`. Both approaches have pros and cons: Pros of `instanceof Array`: * This approach is more efficient, as it only checks if the object is an instance of the `Array` constructor function. * It's also a more straightforward check. Cons of `instanceof Array`: * It may not work correctly for nested arrays or objects with array-like properties. * It may be slower than `Array.isArray()` for large arrays, as it requires a type check. Pros of `Array.isArray()` * This approach is safer and more robust, as it checks if the object is an actual array, regardless of its nesting or properties. * It's also faster than `instanceof Array` for large arrays, as it uses a optimized algorithm. Cons of `Array.isArray()`: * It's slower than `instanceof Array` due to the additional overhead of calling a method on the `Array` prototype. * It may be slower for very small arrays or single-element arrays. **Other considerations** The benchmark also considers other factors, such as: * The size of the array: A larger array will likely impact performance more than a smaller one. * The type of objects being checked: Checking nested arrays or objects with array-like properties may slow down `instanceof Array` while speeding up `Array.isArray()`. * Browser and platform differences: Different browsers and platforms may have different performance characteristics, which can affect the results. **Libraries and special JS features** The benchmark uses the built-in `Array` prototype method `Array.isArray()` to check if an object is an array. There are no external libraries or special JavaScript features being used in this benchmark. **Alternatives** Other approaches to checking if an object is an array include: * Using a regular expression: `!/^\[.*\]$/.test(object)` (not recommended due to performance overhead) * Using a utility function: e.g., `isArray(object) { return Array.isArray(object); }` (similar to `Array.isArray()`, but implemented as a separate function) Keep in mind that the best approach depends on the specific use case and requirements. If you need to check if an object is an array for performance-critical code, using `instanceof Array` or `Array.isArray()` may be a good choice. However, if you need to handle nested arrays or objects with array-like properties, `Array.isArray()` is likely a better option.
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?