Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
checking empty array
(version: 0)
isArray vs array.length
Comparing performance of:
useing isArray vs useing length
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const arr = [{ a: 2, b: 4 }, { a: 3, b: 6 }, { a: 5, b: 9 }] const emptyArr = []
Tests:
useing isArray
const arr = [{ a: 2, b: 4 }, { a: 3, b: 6 }, { a: 5, b: 9 }] const emptyArr = []; if(Array.isArray(arr)) console.log('array'); if(Array.isArray(emptyArr)) console.log('emptyArray');
useing length
const arr = [{ a: 2, b: 4 }, { a: 3, b: 6 }, { a: 5, b: 9 }]; const emptyArr = []; if(arr?.length) console.log('array'); if(emptyArr?.length) console.log('emptyArray');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
useing isArray
useing length
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 break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance difference between two approaches to check if an object is an array: using `Array.isArray()` or checking the length of the object using the dot notation (`?.length`). **Approach 1: Using `Array.isArray()`** This approach checks if the input object is an instance of the `Array` class, which is a built-in JavaScript class. The `Array.isArray()` method returns a boolean value indicating whether the input is an array. Pros: * Simple and straightforward * Easy to read and understand Cons: * Can be slower than other approaches due to the overhead of checking the constructor name * May not work as expected with objects that have a `constructor` property set to `Array` **Approach 2: Using Length Check (`?.length`)** This approach checks if the input object has a `length` property, which is typically present in arrays. The dot notation (`?.`) is used to safely access the `length` property, returning `undefined` if it's not present. Pros: * Often faster than using `Array.isArray()` * More concise and readable Cons: * May fail for objects that don't have a `length` property (e.g., dates, regex objects) * Requires modern JavaScript features (ECMAScript 2018+) **Library Used: None** There are no external libraries used in this benchmark. **Special JS Features/Syntax: None** No special JavaScript features or syntax are being tested in this benchmark. The focus is solely on comparing two basic approaches to check if an object is an array. **Other Alternatives** Some alternative approaches to check if an object is an array include: * Using `instanceof Array` (which is similar to using `Array.isArray()`) * Checking the type of the object using `typeof arr === 'object'` * Creating a new instance of `Array` and comparing it with the input object using `arr instanceof Array` However, these alternatives are not included in the provided benchmark. **Benchmark Preparation Code** The benchmark preparation code creates an array of objects (`const arr`) and an empty array (`const emptyArr`). The script then checks if both arrays satisfy the condition using the two approaches being compared.
Related benchmarks:
JS array emptiness check
var is a non-empty array (v3)
check if array is empty or not using length and at method
Array.from() vs new Array() vs []
Comments
Confirm delete:
Do you really want to delete benchmark?