Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set.has v.s Array.includes
(version: 0)
Comparing performance of:
set vs array
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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);
Tests:
set
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))
array
target1.some(id => ids1.includes(id)) target2.some(id => ids2.includes(id)) target3.some(id => ids3.includes(id))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set
771536.9 Ops/sec
array
11667516.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**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 size**: If the array is small (less than 100 elements), `Array.includes()` might be sufficient. For larger arrays, the performance difference becomes more significant. * **Code readability and maintainability**: Using a `Set` data structure can make the code more concise and easier to read, especially for large arrays. * **Browser support**: Both `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: * **Using a binary search algorithm**: This would allow for O(log n) time complexity, making it even faster than the current `Set` approach. * **Using a custom data structure**: Designing a custom data structure optimized for membership testing could potentially outperform both `Array.includes()` and `has()`. * **Compiling to machine code**: Some JavaScript engines, like V8, can compile JavaScript to native machine code. This would allow for further performance optimizations. Keep in mind that these alternative approaches might require more significant changes to the benchmarking logic or may not be supported by all browsers.
Related benchmarks:
Fisher-Yates Shuffle
yoooooo
Set.has v.s Array.includes v2
Transform dict values: for vs fromEntries 2
Comments
Confirm delete:
Do you really want to delete benchmark?