Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isArray vs instanceof vs typeof
(version: 0)
Comparing performance of:
typeof vs isArray vs instanceof
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var test = [1,2,3,4]; var c;
Tests:
typeof
if (typeof test === 'object') { c++; }
isArray
if (Array.isArray(test)) { c++; }
instanceof
if (test instanceof Array) { c++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
typeof
isArray
instanceof
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof
4516910.0 Ops/sec
isArray
4514873.0 Ops/sec
instanceof
4432167.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare three different approaches: 1. `typeof` 2. `Array.isArray` 3. `instanceof` All three methods are used to check if a variable (`test`) is an object or an array. **Pros and Cons of each approach:** * `typeof`: This method checks the type of a variable at runtime, using a special operator in JavaScript. It returns one of the following strings: + `"object"` for objects + `"number"`, `"string"`, etc. for other numeric or string types + `"undefined"` for uninitialized variables * `Array.isArray`: This method is specifically designed to check if an object is an array, using a static property on the `Array` constructor. + Pros: Efficient and specific to arrays only. + Cons: May return false positives (e.g., checking an object with a numeric property named "0"). * `instanceof`: This method checks if an object is an instance of a particular constructor function. In this case, it's used to check if `test` is an instance of the `Array` constructor. + Pros: More specific than `typeof` and can handle arrays with non-numeric property names. + Cons: May be slower due to the overhead of constructing the `Array` object. **Library usage** None of the methods require any external libraries. **Special JavaScript feature or syntax** The benchmark uses a special feature of JavaScript called "strict equality" (`===`), which is used to compare values without considering type conversions. This is not explicitly mentioned in the benchmark, but it's implied by using `typeof` and `instanceof`. **Other alternatives** There are other ways to check if an object or array in JavaScript: * Using a regular expression: `test instanceof RegExp.test && test.value === undefined` * Using the `constructor` property: `test.constructor.name === 'Array'` * Using a function call with a specific constructor: `new Array(test).length !== 0` However, these alternatives are not included in this benchmark. **Benchmark preparation code** The script preparation code creates an array `test` and assigns it to the variable `c`, which is then used as input for each test case. The `var c;` statement at the end of the script preparation code creates a new variable `c`, which is not referenced anywhere in the benchmark. This is likely done to ensure that the benchmark produces accurate results, even if the variable `c` is not used elsewhere. **Individual test cases** Each test case uses one of the three approaches mentioned earlier: 1. `typeof`: Checks if `test` has an `"object"` type. 2. `Array.isArray`: Checks if `test` is a specific instance of the `Array` constructor. 3. `instanceof`: Checks if `test` is an instance of the `Array` constructor. The benchmark result shows the execution time for each test case in different browsers, with `typeof` performing slightly better than `Array.isArray` and `instanceof`.
Related benchmarks:
Array isArray vs instanceof
Array isArray vs instanceof 2
Array isArray vs typeof
instanceof Array vs Array.isArray
Comments
Confirm delete:
Do you really want to delete benchmark?