var list = ['aaa', 'bbb', 'ccc'];
var exists = list.includes('abc');
var item = list.find(v => v === 'abc');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.includes | |
Array.find |
Test name | Executions per second |
---|---|
Array.includes | 16654104.0 Ops/sec |
Array.find | 17671246.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark, titled "#2 Array Includes vs. Find", tests the performance difference between two JavaScript array methods: includes()
and find()
. The benchmark is designed to measure which method is more efficient when searching for a specific element within an array.
Options Compared
Two options are being compared:
Array.includes()
: This method returns true
if an array contains the specified value, or false
otherwise.Array.find()
: This method returns the first element in the array that satisfies the provided testing function, or undefined
if no elements satisfy it.Pros and Cons of Each Approach
Array.includes()
:find()
for large arrays because it may require a linear search through the array.Array.find()
:includes()
, allowing for more complex search conditions.includes()
for small arrays due to the overhead of creating and executing a callback function.Library/Functionality Used
In this benchmark, no libraries or external functions are used beyond the built-in JavaScript array methods. However, it's worth noting that some modern browsers may have additional features or optimizations enabled for these methods, which could affect performance.
Special JS Feature/Syntax
There is no special JavaScript feature or syntax being tested in this benchmark. The focus is solely on comparing the performance of includes()
and find()
.
Other Alternatives
If you're looking to optimize array searches, here are some alternative approaches:
Keep in mind that these alternatives may require more code and are generally less straightforward than using built-in array methods like includes()
and find()
.