Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Set.has v2
(version: 0)
Comparing performance of:
Array.includes vs Set.has
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArr = []; for(let i = 0; i < 30; i++) { testArr.push(i); } var testSet = new Set(testArr); var valuesToCheck = [1, 10, 20, 30, 40 ,50];
Tests:
Array.includes
let summ = 0; valuesToCheck.forEach(v => { if (testArr.includes(v)) { summ += v; } }); return summ;
Set.has
let summ = 0; valuesToCheck.forEach(v => { if (testSet.has(v)) { summ += v; } }); return summ;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.includes
Set.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **What is being tested?** The benchmark compares two JavaScript methods for checking if an element exists in a collection: `Array.includes` and `Set.has`. Specifically, it measures the performance of these methods when used to sum up all elements from 1 to 50 that exist in either an array or a set. **Options compared:** Two options are being compared: 1. **Array.includes**: This method checks if a given element exists in an array. 2. **Set.has**: This method checks if a given element has a presence in a set. **Pros and Cons of each approach:** **Array.includes:** Pros: * Widely supported across JavaScript engines * Efficient for small to medium-sized datasets Cons: * Has to iterate through the entire array, which can be slow for large datasets * May not be as cache-friendly as other methods (e.g., Set.has) **Set.has:** Pros: * Much faster than Array.includes for large datasets, as it only needs to check if the element exists in the set * Cache-friendly, as most modern browsers store sets in memory Cons: * Not as widely supported across JavaScript engines (some older engines may not support sets or have limited set functionality) * May not be suitable for small datasets where the overhead of creating a set is too high **Library and purpose:** In this benchmark, `Set` is used. A `Set` is a collection of unique values that can be added to it, without duplicates. The purpose of using a `Set` here is to demonstrate the performance difference between Array.includes and Set.has. **Special JS feature or syntax:** This benchmark does not use any special JavaScript features or syntax beyond what's required for the basic operations. However, it's worth noting that some older browsers may have limited set support due to security concerns, but this should not affect modern browsers running this benchmark. **Other alternatives:** Alternative methods for checking if an element exists in a collection include: * `Array.prototype.indexOf()`: Similar to Array.includes but returns the index of the first occurrence instead of a boolean value. * `WeakSet.has()`: A method similar to Set.has, but optimized for use with WeakSets (sets that can contain weak references). Keep in mind that these alternatives might have slightly different performance characteristics or behaviors depending on the specific use case and JavaScript engine.
Related benchmarks:
Array.from vs. ... expansion
array.includes vs. set.has on the fly
Array includes vs Set.has
set vs array includestratsatsrats
new set vs array includes
Comments
Confirm delete:
Do you really want to delete benchmark?