var arr = Array.from(100).fill().map((_, index) => index);
var set = new Set(arr);
arr.includes(5);
set.has(5);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
has |
Test name | Executions per second |
---|---|
includes | 15237489.0 Ops/sec |
has | 13698684.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net!
The provided benchmark tests two different methods for checking if an element exists in a sequence or set: includes()
and has()
. These methods are used to determine whether a specific value is present in an array, set, or other sequences.
Options Compared
In this benchmark, the following options are compared:
arr.includes(5)
: This method checks if the value 5
exists in the arr
array using the includes()
function.set.has(5)
: This method checks if the value 5
exists in the set
set using the has()
function.Pros and Cons
arr.includes(5)
:set.has(5)
:arr.includes()
for very small arrays.Library/Function Purpose
The Array.prototype.includes()
function and the Set.prototype.has()
method are built-in JavaScript functions that are used to check for the presence of a value in an array or set, respectively.
includes()
Function: This function returns true
if an element with the specified value exists in the array; otherwise, it returns false
.has()
Method: This method returns a boolean indicating whether the element with the specified key exists in the set.Special JS Features/Syntax
In this benchmark, there is no use of special JavaScript features or syntax such as arrow functions, async/await, or modern JavaScript APIs like Promise.all() or fetch().
Other Alternatives
If you're looking for alternative methods to check for element existence in an array or set, consider the following:
arr.indexOf(5)
: This method returns the index of the first occurrence of 5
in the array. If 5
is not found, it returns -1
.has()
method.In conclusion, the provided benchmark on MeasureThat.net compares two common methods for checking element existence in an array: includes()
and has()
. The choice between these methods depends on your specific use case, performance requirements, and the size of your dataset.