Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isArray vs instanceOf vs Symbol.iterator vs Object.prototype.toString.call.slice
(version: 0)
Symbol.iterator also matches TypedArray, Map, and such
Comparing performance of:
isArray vs instanceof vs Symbol.iterator vs Object.prototype.toString.call
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var types = { array: [1,2,3], number: 123, string: '123', map: new Map([[1,1],[2,2],[3,3]]), set: new Set([1,2,3]), buffer: new ArrayBuffer([1,2,3]), boolean: true, arrow: () => {}, function: function () {}, object: {}, u8: new Uint8Array(), u16: new Uint16Array(), u32: new Uint32Array(), i8: new Int8Array(), i16: new Int16Array(), i32: new Int32Array() } var keys = Object.keys(types)
Tests:
isArray
keys.map(key => Array.isArray(types[key]) === true)
instanceof
keys.map(key => (types[key] instanceof Array) === true)
Symbol.iterator
keys.map(key => types[key][Symbol.iterator] !== undefined)
Object.prototype.toString.call
keys.map(key => Object.prototype.toString.call(types[key]).slice(8, -1) == 'Array')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
isArray
instanceof
Symbol.iterator
Object.prototype.toString.call
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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided JSON represents a benchmark test on `MeasureThat.net` that compares four different approaches to check if a value is an array in JavaScript: 1. `Array.isArray()` 2. `instanceof Array` 3. `Symbol.iterator` (also checks for TypedArray, Map, and Set) 4. `Object.prototype.toString.call()` with a slice manipulation **Options Compared** Each option is compared using the following test cases: * `keys.map(key => ... === true)` to check if the condition returns `true` in each case * The results are then displayed as a raw UA string, which represents the browser's User Agent and version Here's a brief explanation of each option: 1. **`Array.isArray()`**: This method takes an array-like object (such as a TypedArray, Map, or Set) and returns `true` if it is an actual array. 2. **`instanceof Array```**: This checks if the value is an instance of the `Array` constructor function. 3. **`Symbol.iterator`**: In JavaScript, arrays have a `Symbol.iterator` property that returns an iterator object. Checking for this property (or its presence) can help identify whether a value is an array-like object. 4. **`Object.prototype.toString.call()` with slice manipulation**: This method uses the `toString()` method of the prototype chain to get a string representation of the object, and then uses slicing (`slice(8, -1)`) to extract the type of the object from the string. **Pros and Cons** Here's a brief summary of each option: 1. **`Array.isArray()`**: * Pros: Simple and efficient. * Cons: May return `false` for TypedArrays or other array-like objects that don't have an `array` property. 2. **`instanceof Array```**: * Pros: More accurate than `Array.isArray()` for checking if a value is actually an array. * Cons: Returns `true` for any object that is a subclass of `Array`, which might not be desirable. 3. **`Symbol.iterator`**: * Pros: Covers TypedArrays, Maps, and Sets, in addition to arrays. * Cons: May return `false` if the value doesn't have an iterator symbol (although this is rare). 4. **`Object.prototype.toString.call()` with slice manipulation**: * Pros: Can cover a wide range of object types by checking the prototype chain. * Cons: Slicing and string manipulation can be less efficient than simple checks like `Array.isArray()`.
Related benchmarks:
isArray vs instanceof vs Symbol.iterator
isArray vs instanceof vs Symbol.iterator vs Object.prototype.toString.call
instanceof vs .prototype.isPrototypeOf vs Object.prototype.toString.call vs typeof vs isArray vs Symbol.iterator
t9834yt9h43
Comments
Confirm delete:
Do you really want to delete benchmark?