Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
var is a non-empty array (v3)
(version: 0)
Micro-benchmark various method of checking for non-empty array on arbitrary variable
Comparing performance of:
IsArray vs typecheck (nullable dot) (array) vs manual check cond vs typeof obj + length vs manual check with null-coalescing
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [1,2,3]; var notArr = null;
Tests:
IsArray
const a = Array.isArray(arr) && arr.length > 0; const b = Array.isArray(notArr) && notArr.length > 0;
typecheck (nullable dot) (array)
const a = arr?.length > 0 && arr[0] !== undefined; const b = notArr?.length > 0 && notArr[0] !== undefined;
manual check cond
const a = arr !== null && arr !== undefined && arr.length > 0 && arr[0] !== undefined; const b = notArr !== null && notArr !== undefined && notArr.length > 0 && notArr[0] !== undefined;
typeof obj + length
const a = typeof arr === 'object' && arr !== null && arr.length > 0 && arr[0] !== undefined; const b = typeof notArr === 'object' && notArr !== null && notArr.length > 0 && notArr[0] !== undefined;
manual check with null-coalescing
const a = (arr ?? []).length > 0 && arr[0] !== undefined; const b = (notArr ?? []).length > 0 && notArr[0] !== undefined;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
IsArray
typecheck (nullable dot) (array)
manual check cond
typeof obj + length
manual check with null-coalescing
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!
Related benchmarks:
JS array emptiness check
var is a non-empty array
var is a non-empty array (v2)
check if array is empty or not using length and at method
Comments
Confirm delete:
Do you really want to delete benchmark?