var data = []
for (let i = 0; i < 10000; ++i) data.push('toto')
data.push('titi')
for (let i = 0; i < 2500; ++i) data.push('toto')
data.includes('titi')
data.some(x=> x === "titi")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Includes | |
Some |
Test name | Executions per second |
---|---|
Includes | 14344.1 Ops/sec |
Some | 8550.2 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two ways to check if an element exists in an array: using the includes()
method versus using the some()
method with a callback function.
What are we testing?
We're testing how fast each of these methods performs on a specific test case:
data.includes('titi')
)some()
(data.some(x => x === "titi")
)Options being compared
Two options are being compared:
includes()
method: This method checks if an element exists in the array by iterating over the elements and checking for a match.some()
method with callback function: This method checks if any element in the array satisfies the condition specified in the callback function. In this case, the condition is x === "titi"
.Pros and Cons of each approach
includes()
method:some()
method with callback function:Other considerations
The benchmark also measures the execution speed of each test case on different devices and browsers. This provides a more comprehensive understanding of performance variations across different hardware and software configurations.
Now, let's look at some specific libraries or features mentioned in the benchmark:
data
array: A JavaScript array created using var data = []
. It contains 30,000 elements.No special JS features or syntax are being tested in this benchmark.
As for alternatives, other ways to check if an element exists in an array include:
forEach()
loop with a conditional statementin
operator_.includes()
) or Ramda (R.includes()
)However, these alternatives are not being tested in this specific benchmark.
Keep in mind that performance differences between these methods can be significant for large arrays or complex data structures. The choice of method ultimately depends on the specific use case and personal preference.