Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Set.has v3
(version: 0)
Comparing performance of:
Array.includes vs Set.has
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArr = []; for(let i = 0; i < 100000; i++) { testArr.push(i); } var testSet = new Set(testArr); var valuesToCheck = [0, 10, 200, 3000, 40000 ,500000];
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:
Run details:
(Test run date:
2 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
103441.7 Ops/sec
Set.has
32683498.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, the options compared, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark definition is a JSON object that describes two test cases: 1. "Array.includes" 2. "Set.has v3" The script preparation code generates an array `testArr` with 100,000 elements and creates a set `testSet` from this array. The `valuesToCheck` array contains five values to check for membership. **Options Compared** Two options are compared: 1. **Array.includes**: This method checks if a value exists in the array. 2. **Set.has**: This method checks if a value exists in the set. **Approaches and Considerations** Both approaches have pros and cons: **Array.includes:** Pros: * Widely supported by most browsers * Simple to implement Cons: * Can be slower for large arrays due to the need to scan through elements * Can lead to unnecessary iterations if the value is not found in the array **Set.has:** Pros: * Faster lookup times compared to Array.includes, especially for large sets * More efficient use of memory since sets automatically eliminate duplicates Cons: * Not all browsers support the Set API (e.g., older versions of Chrome) * Requires additional code to create and manage the set In general, Set.has is a better approach when dealing with large datasets or performance-critical applications. However, Array.includes may be preferred in situations where simplicity and minimal code changes are more important. **Library/Function:** The Set API is used in the "Set.has" test case. The Set API is a built-in JavaScript object that allows you to store unique values, eliminating duplicates. It's implemented as an iterator, which makes it efficient for lookup operations. **Special JS Feature/Syntax:** There are no special JavaScript features or syntaxes being tested in this benchmark. Both Array.includes and Set.has use standard JavaScript methods. **Alternatives:** If you need to compare the performance of other membership tests, such as: * `Array.indexOf()`: This method returns the index of a value within an array. If the value is not found, it returns -1. * `Object.contains()`: This method is part of the ECMAScript 2015 (ES6) standard and checks if a property exists in an object. * `Map.has()`: Similar to Set.has, but for Map objects. Keep in mind that these alternatives may have different performance characteristics or be supported by only certain browsers.
Related benchmarks:
set.has vs. array.includes large 3
Array.from vs. ... expansion
JS includes vs set
set.has vs. array.includes - large array
set vs array iteration 100k elements
Comments
Confirm delete:
Do you really want to delete benchmark?