[1,2, 3, 4, 5].indexOf(4) !== -1
[1,2, 3, 4, 5].filter(a=> a ===4).length > 0
[1,2, 3, 4, 5].find(a => a === 4) !== undefined
[1,2, 3, 4, 5].some( a => a === 4)
![1,2, 3, 4, 5].every( a => a !== 4)
[1,2, 3, 4, 5].lastIndexOf(4) !== -1
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
indexOf !== | |
filter length > 0 | |
find !== undefined | |
some | |
! every | |
lastIndexOf !== -1 |
Test name | Executions per second |
---|---|
indexOf !== | 117868936.0 Ops/sec |
filter length > 0 | 28376626.0 Ops/sec |
find !== undefined | 6315988.0 Ops/sec |
some | 158655856.0 Ops/sec |
! every | 165978736.0 Ops/sec |
lastIndexOf !== -1 | 47638876.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is being tested?
The benchmark tests the performance of different methods to check if an element exists in an array:
indexOf()
method: Returns the index of the first occurrence of the specified value, or -1 if it's not found.filter()
: Creates a new array with all elements that pass the test implemented by the provided function.find()
: Returns the value of the first element in an array that satisfies the provided testing function.some()
: Returns true if at least one element in the array passes the test implemented by the provided function.every()
: Returns true if all elements in the array pass the test implemented by the provided function.lastIndexOf()
method: Returns the index of the last occurrence of the specified value, or -1 if it's not found.Options compared
The benchmark compares different approaches to check if an element exists in an array:
indexOf()
with a non-existent value (returns -1) vs. using indexOf()
with an existing value.filter()
and checking its length vs. directly accessing the element.find()
vs. other methods like indexOf()
, some()
, or every()
.lastIndexOf()
to find the last occurrence of an element.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
indexOf()
: Pros: simple, widely supported; Cons: can be slower for large arrays, returns -1 for non-existent values.filter()
: Pros: efficient for creating new arrays; Cons: creates a new array, which can be memory-intensive.find()
: Pros: efficient for finding the first occurrence of an element; Cons: may not work as expected if no elements match.some()
: Pros: simple and concise; Cons: returns true for any single matching element, not just the first one.every()
: Pros: efficient for checking all elements in an array; Cons: returns false if a single element fails the test.lastIndexOf()
: Pros: similar to indexOf()
but returns the last occurrence; Cons: less widely supported.Library usage
None of the benchmark tests use any external libraries, so we can assume that the JavaScript engine being used is standard JavaScript without any additional dependencies.
Special JS features or syntax
The benchmark does not use any special JavaScript features or syntax, such as async/await
, let
or const
declarations, or functional programming constructs like arrow functions. The tests are written in a straightforward imperative style.
Other alternatives
If you're interested in exploring alternative approaches to check if an element exists in an array, here are some additional methods:
includes()
: Introduced in ECMAScript 2019, this method returns true if the specified value is found in the array.map()
and checking for empty results: You can use map()
to create a new array with the same number of elements as the original array. If no elements match the test function, an empty array will be returned.Keep in mind that these alternative approaches may have different performance characteristics or require additional resources, so it's essential to consider your specific use case and requirements when choosing an approach.