function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function createRandomIntArray(minLength, maxLength) {
const length = getRandomInt(minLength, maxLength);
const randomArray = [];
for (let i = 0; i < length; i++) {
const randomInt = i;
randomArray.push(getRandomInt(0, maxLength));
}
return randomArray;
}
const minLength = 1;
const maxLength = 100;
var ids1 = createRandomIntArray(minLength, maxLength);
var ids2 = createRandomIntArray(minLength, maxLength);
var ids3 = createRandomIntArray(minLength, maxLength);
var target1 = createRandomIntArray(1, 50);
var target2 = createRandomIntArray(1, 50);
var target3 = createRandomIntArray(1, 50);
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 | 313273.5 Ops/sec |
array | 2537460.0 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of two approaches: using Array.includes()
versus using a Set
data structure.
Test Case 1: "set"
This test case creates three random arrays with lengths between 1 and 100, and then checks if each array contains any element from another randomly generated array. The test uses a Set
data structure to store the first array and checks for membership using the has()
method.
Test Case 2: "array"
This test case is similar to Test Case 1, but instead of using a Set
, it checks for membership using the includes()
method on the original arrays.
Comparison and Analysis
The two approaches have different pros and cons:
Array.includes()
: This method has a time complexity of O(n), which means it will perform poorly if the array is large. However, it is a simple and widely supported method that works well for small to medium-sized arrays.Set
data structure with has()
: This approach has an average time complexity of O(1), making it much faster than Array.includes()
for large arrays. However, creating a new Set
object can be slower than simply iterating over the array.Other Considerations
When choosing between these approaches, consider the following factors:
Array.includes()
might be sufficient. For larger arrays, the performance difference becomes more significant.Set
data structure can make the code more concise and easier to read, especially for large arrays.Array.includes()
and has()
are widely supported across modern browsers.Library and Special JS Features
No specific libraries or special JavaScript features are used in this benchmark. However, it's worth noting that using a Set
data structure relies on the Map
interface, which is also widely supported.
Alternative Approaches
Other approaches to achieve similar performance could include:
Set
approach.Array.includes()
and has()
.Keep in mind that these alternative approaches might require more significant changes to the benchmarking logic or may not be supported by all browsers.