function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function createRandomIntArray(maxLength) {
const randomArray = [];
for (let i = 0; i < maxLength; i++) {
const randomInt = i;
randomArray.push(i);
}
return randomArray;
}
function createTargetArray(maxLength, maxValue) {
const randomArray = [];
for (let i = 0; i < maxLength-1; i++) {
const randomInt = i;
randomArray.push(10001+i);
}
randomArray.push(maxValue);
return randomArray;
}
var ids1 = createRandomIntArray(100);
var ids2 = createRandomIntArray(1000);
var ids3 = createRandomIntArray(10000);
var target1 = createTargetArray(100, 99);
var target2 = createTargetArray(1000, 999);
var target3 = createTargetArray(10000, 9999);
var idsSet1 = new Set(ids1)
target1.some(id => idsSet1.has(id))
var idsSet2 = new Set(ids2)
target2.some(id => idsSet2.has(id))
var idsSet3 = new Set(ids3)
target3.some(id => idsSet3.has(id))
target1.some(id => ids1.includes(id))
target2.some(id => ids2.includes(id))
target3.some(id => ids3.includes(id))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
set | |
array |
Test name | Executions per second |
---|---|
set | 1738.1 Ops/sec |
array | 89.2 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Definition
The benchmark is comparing two methods to check if an element exists in an array or a Set:
Set.has()
methodArray.includes()
method (specifically, the new version of includes()
introduced in ECMAScript 2020, which allows passing a callback function).Script Preparation Code
The script preparation code generates three arrays with random integers and one array with a target value. The arrays are created using two functions:
createRandomIntArray(maxLength)
: creates an array with maxLength
elements, where each element is a random integer between 0 and maxLength - 1
.createTargetArray(maxLength, maxValue)
: creates an array with maxLength
elements, where the last element is set to maxValue
.Benchmark Definition Json
The benchmark definition json contains two test cases:
Set.has()
method to check if each element in the arrays exists in the Set.Array.includes()
to check if each element in the arrays exists.Options Compared
The benchmark is comparing two options:
Set.has()
to check for existenceArray.includes()
with a callback functionPros and Cons
Using Set.has()
:
Array.includes()
Using the new version of Array.includes()
with a callback function:
Set.has()
, as it can handle more complex conditionsSet.has()
Library Used
The benchmark uses the JavaScript Set
data structure, which is a collection of unique values. The Set data structure provides an efficient way to check for existence using the has()
method.
Special JS Feature/Syntax
There are no special JavaScript features or syntax used in this benchmark, aside from the new version of Array.includes()
introduced in ECMAScript 2020.
Other Alternatives
If you're interested in exploring alternative methods for checking existence in arrays or Sets, here are a few options:
Object.prototype.hasOwnProperty.call()
: This method can be used to check if an element exists in an array or object, but it may not be the most efficient or readable solution.Keep in mind that the choice of method will depend on your specific use case and performance requirements.