Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isArray vs instanceof vs Symbol.iterator
(version: 0)
Symbol.iterator also matches TypedArray, Map, and such
Comparing performance of:
isArray vs instanceof vs Symbol.iterator
Created:
7 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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
isArray
instanceof
Symbol.iterator
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
isArray
689428.9 Ops/sec
instanceof
662279.9 Ops/sec
Symbol.iterator
422943.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark tests three different approaches to check if a variable is an array: `isArray`, `instanceof Array`, and `Symbol.iterator`. **Options Compared** 1. `isArray`: This method uses the built-in `Array.isArray()` function to check if a value is an array. 2. `instanceof Array`: This method checks if an object's prototype chain includes `Array`. 3. `Symbol.iterator`: This method checks if an object has a `Symbol.iterator` property. **Pros and Cons of Each Approach** 1. **isArray**: Pros: * Fast and efficient, as it uses a built-in function. * Easy to read and understand. Cons: * May not work correctly for all types of arrays (e.g., TypedArray, Map). 2. **instanceof Array**: Pros: * Works correctly for all types of arrays. Cons: * Slower than `isArray` due to the prototype chain lookup. * Less readable and maintainable due to its complexity. 3. **Symbol.iterator**: Pros: * Fast and efficient, as it only checks if a property exists. * More modern and flexible approach. Cons: * Requires JavaScript 10 or later for `Symbol.iterator` support. **Library Used** None are used in this benchmark. However, the test cases use `Object.keys()` to iterate over the object properties. **Special JS Feature/Syntax** The benchmark uses the `Symbol.iterator` feature, which was introduced in JavaScript 10. This allows objects with a `Symbol.iterator` property to be iterated over using the `for...of` loop or the `Array.from()` method. **Other Considerations** * The test cases use an object with various types of arrays and other data structures (e.g., Map, Set, ArrayBuffer) to ensure the tests are comprehensive. * The benchmark runs on a Chrome browser (127.0.0.0) and provides execution times per second for each test case. **Alternatives** If you need to create similar benchmarks, consider using: 1. **Benchmarking libraries**: Such as Benchmark.js or micro-benchmark, which provide more features and flexibility. 2. **Custom testing frameworks**: Like Jest or Mocha, which offer more comprehensive testing capabilities. 3. **JavaScript testing tools**: Such as Jest's `benchmark` package or the `js benchmark` tool, which provide more advanced testing options. Keep in mind that these alternatives may have different requirements, usage patterns, and performance characteristics compared to MeasureThat.net's simple micro-benchmarking framework.
Related benchmarks:
isArray vs instanceof vs Symbol.iterator vs Object.prototype.toString.call
isArray vs instanceOf vs Symbol.iterator vs Object.prototype.toString.call.slice
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?