Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array isArray vs typeof (negative case)
(version: 1)
Comparing performance of:
typeof vs isArray
Created:
one year 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++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
typeof
isArray
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof
101713664.0 Ops/sec
isArray
87162336.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided tests the performance of two different methods for determining if a variable is an array in JavaScript: using `typeof` and using the static method `Array.isArray()`. Below, we will explore what each approach involves, their pros and cons, and other considerations. ### Benchmark Overview **1. Test Cases:** - **Test Name: `typeof`** - Benchmark Definition: `if (typeof test === 'object') { c++; }` - **Test Name: `isArray`** - Benchmark Definition: `if (Array.isArray(test)) { c++; }` ### Options Compared - **`typeof` Operator:** - **Description:** The `typeof` operator in JavaScript returns a string indicating the type of the unevaluated operand. Here, it is used to check if `test` is of the type 'object'. Since arrays are technically a type of object in JavaScript, this check may return true for arrays. - **`Array.isArray()` Method:** - **Description:** `Array.isArray()` is a static method that returns `true` if the argument passed to it is an array, and `false` otherwise. This method is specifically designed to check for arrays and is generally more reliable than using `typeof`. ### Pros and Cons - **Using `typeof`:** - **Pros:** - Quick and straightforward syntax. - Works with fewer lines of code. - **Cons:** - Can produce false positives: It returns true for any object, not just arrays. For example, it will return `true` for objects, dates, and other non-array objects. - **Using `Array.isArray()`:** - **Pros:** - Specifically checks for arrays, providing more accurate results. - Only returns `true` for arrays, eliminating false positives. - **Cons:** - Introduced in ECMAScript 5, which may not be supported in very old environments or browsers. - Slightly more verbose than using `typeof`. ### Performance Considerations Based on the benchmark results: - **Execution Speed:** - `typeof` achieved approximately **74,057,608 executions per second**. - `Array.isArray()` achieved approximately **72,110,464 executions per second**. In this specific context, `typeof` performs slightly better than `Array.isArray()`. However, the performance difference may be negligible in most real-world applications, and the choice may be based more on correctness rather than benchmark speed. ### Alternatives There are other methods to check if a variable is an array including: 1. **Instanceof Operator:** - Example: `if (test instanceof Array) { c++; }` - **Pros:** Checks if `test` is an instance of the Array constructor. - **Cons:** Can fail if the code is running in different execution contexts (e.g., iframes) because each context may have its own Array object. 2. **Object.prototype.toString.call:** - Example: `if (Object.prototype.toString.call(test) === '[object Array]') { c++; }` - **Pros:** Works in all contexts and provides reliable results across different environments. - **Cons:** More complex syntax, lower readability for those unfamiliar with the method. ### Conclusion When determining if a variable is an array in JavaScript, both `typeof` and `Array.isArray()` have their use cases. While `typeof` may offer slightly better performance in this benchmark, `Array.isArray()` is the preferred method for accurately identifying arrays without the risk of false positives. Depending on the specific needs of your project—especially with regard to compatibility and correctness—using `Array.isArray()` is often the better choice. Additionally, understanding alternative methods can help cater to specific project requirements or environments.
Related benchmarks:
Array isArray vs instanceof
Array isArray vs instanceof 2
Array isArray vs instanceof vs isEqual vs isLessThan
Array isArray vs typeof
Array isArray vs 'in'
instanceof Array vs Array.isArray
isArray vs instanceof vs typeof
typeof vs isArray
barbar
Array isArray vs Object.prototype
Comments
Confirm delete:
Do you really want to delete benchmark?