Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
t9834yt9h43
(version: 0)
Comparing performance of:
isArray vs instanceof vs Object.prototype.toString.call vs type
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var type = (value) => Object.prototype.toString.call(value).slice(8, -1); 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)
Object.prototype.toString.call
keys.map(key => Object.prototype.toString.call(types[key]) === '[object Array]')
type
keys.map(key => type(types[key]) === 'Array')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
isArray
instanceof
Object.prototype.toString.call
type
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
isArray
2048784.1 Ops/sec
instanceof
1745728.9 Ops/sec
Object.prototype.toString.call
598002.0 Ops/sec
type
383289.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance can be a fascinating topic. **Benchmark Definition:** The benchmark definition is a JSON object that contains the setup code for each test case. It defines a `types` object with various data types (arrays, numbers, strings, maps, sets, booleans, and functions) and extracts their keys using `Object.keys(types)`. In essence, this benchmark tests the performance of different JavaScript operators on arrays: * `Array.isArray()` * `instanceof Array` (not directly used in the benchmark, but inferred from `instanceof`) * `Object.prototype.toString.call()` with `[object Array]` as a string literal * `type()` function, which returns the constructor name of an object **Options Compared:** The benchmark compares the performance of these four different approaches: 1. `Array.isArray()` 2. `instanceof Array` (not directly used in the benchmark) 3. `Object.prototype.toString.call()` with `[object Array]` as a string literal 4. `type()` function **Pros and Cons:** Here's a brief overview of each approach: * **`Array.isArray()`**: This method is efficient and widely supported across browsers. However, it may be slower for very large arrays due to its additional overhead. * **`instanceof Array`**: While this method is more explicit than `Array.isArray()`, it's not directly used in the benchmark. Instead, it's inferred from the context. This approach can be less efficient due to its additional checks and potential overhead. * **`Object.prototype.toString.call()` with `[object Array]` as a string literal**: This approach uses the `toString()` method to check if an object is an array. While it's widely supported, it may incur additional overhead due to the creation of a new string object. * **`type()` function**: The `type()` function is not part of the standard JavaScript API but can be implemented using `Object.prototype.toString.call()`. It provides explicit and efficient checks for array types. **Other Considerations:** In general, performance differences between these approaches are relatively small. However, if you need to optimize specific use cases: * For very large arrays or performance-critical code, consider using the most efficient method (`Array.isArray()`). * If you're working with older browsers that don't support `Array.isArray()`, use a fallback like `instanceof Array`. * Avoid using `Object.prototype.toString.call()` when possible due to its additional overhead. **Library Usage:** The benchmark uses the `type()` function, which is not part of the standard JavaScript API. The implementation of `type()` in this case likely uses `Object.prototype.toString.call()`, making it a wrapper around the built-in method. **Special JS Features/Syntax:** There are no special JavaScript features or syntax mentioned in the benchmark definition.
Related benchmarks:
isArray vs instanceof vs Symbol.iterator
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
Comments
Confirm delete:
Do you really want to delete benchmark?