Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array: Object.prototype.toString vs instanceof vs Array.isArray
(version: 0)
Comparing performance of:
toString vs instanceof vs isArray
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var {toString} = Object.prototype; var arr = [1, 2, 3, 4]; var c =0;
Tests:
toString
if(toString.call(arr) === '[object Array]') {c++}
instanceof
if(arr instanceof Array) {c++}
isArray
if(Array.isArray(arr)) {c++}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
toString
instanceof
isArray
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.4.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
toString
84956104.0 Ops/sec
instanceof
370184256.0 Ops/sec
isArray
127403720.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the JavaScript benchmark on MeasureThat.net. **Benchmark Overview** The benchmark measures the performance difference between three ways to check if an array is an array in JavaScript: using `toString.call(arr) === '[object Array]'`, `arr instanceof Array`, and `Array.isArray(arr)`. **Options Compared** The three options being compared are: 1. **`toString.call(arr) === '[object Array]```**: This method uses the `toString()` function to convert the array to a string, and then compares it to the string representation of `[object Array]`. This is a low-level approach that checks if the array's prototype chain contains the `Array` object. 2. **`arr instanceof Array```**: This method uses the `instanceof` operator to check if an instance of `Array` can be returned by calling `constructor()` on `arr`. This method is more intuitive and straightforward, but might have performance implications depending on the array's prototype chain. 3. **`Array.isArray(arr)```**: This method is a modern approach that uses the `Array` function to check if an object passes the Array constructor test suite. This method is often used in modern JavaScript code, but might be less well-known. **Pros and Cons of Each Approach** 1. **`toString.call(arr) === '[object Array]'```**: * Pros: low overhead, fast execution * Cons: may have performance implications depending on the array's prototype chain, can be brittle if arrays are extended with custom prototypes. 2. **`arr instanceof Array```**: * Pros: intuitive, easy to read and understand * Cons: might have performance implications depending on the array's prototype chain, not all browsers support `instanceof` for objects. 3. **`Array.isArray(arr)```**: * Pros: modern, efficient, robust * Cons: may be less familiar to older developers, requires a more nuanced understanding of the Array constructor test suite. **Library and Special JS Features** The benchmark uses none of any library or special JavaScript feature beyond the standard ECMAScript features. **Other Considerations** When choosing between these approaches, consider the following factors: * Performance: If performance is critical, `toString.call(arr) === '[object Array]'` might be a good choice. However, this approach may have implications for arrays with custom prototypes. * Readability and maintainability: If readability is important, `arr instanceof Array` or `Array.isArray(arr)` might be a better choice due to their more intuitive nature. * Robustness: If you need to handle arrays extended by custom code or older browsers, `Array.isArray(arr)` is a good option. **Alternatives** If these three approaches are not sufficient for your needs, consider using other methods: * **`Object.getPrototypeOf()`**: This method can be used to check if an object's prototype chain contains the `Array` object. * **`Array.prototype.forEach()`**: This method can be used to test whether a function takes at least two arguments. * **`Array.prototype.every()`**: This method can be used to test whether every element in an array passes a certain condition. Keep in mind that these alternatives might have different performance implications or trade-offs depending on the specific use case.
Related benchmarks:
Object vs toString
isarray_vs_instanceof_vs_tostring
Array isArray vs Object.prototype
Array.isArray vs Object.prototype.toString vs [].constructor vs instanceof
Comments
Confirm delete:
Do you really want to delete benchmark?