Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Set.has
(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 < 50; 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. **Benchmark Overview** The benchmark compares the performance of two JavaScript methods: 1. `Array.includes()` (test case: "Array.includes") 2. `Set.has()` (test case: "Set.has") Both tests use an array (`testArr`) and a set (`testSet`) created from the same array. The test loops through a predefined array of values (`valuesToCheck`) and checks if each value is present in either the array or the set using their respective methods. **Options Compared** The benchmark compares two approaches: 1. **Array.includes()**: This method searches for a specific value in an array by iterating over its elements. 2. **Set.has()**: This method checks if a value is present in a set by iterating over its underlying data structure (typically a hash table). **Pros and Cons** * `Array.includes()`: + Pros: Easy to implement, widely supported, and well-documented. + Cons: Iterates over the entire array for each lookup, making it less efficient for large datasets. * `Set.has()`: + Pros: Faster lookup times due to the set's hash table data structure, reducing the number of iterations required. + Cons: Requires creating a set from an array, which can be slower than creating an array and using `includes()`. Also, not all browsers support sets or have optimized implementations. **Library/External Dependencies** In this benchmark, no external libraries are used. The `Set` object is a built-in JavaScript object, which is supported by most modern browsers. No special JavaScript features or syntax are used in these tests. **Other Considerations** When comparing the performance of these two methods, other factors to consider include: * Array size: Larger arrays may favor `Array.includes()` due to its simplicity and wide support. * Browser optimizations: Different browsers might optimize one or both methods differently, affecting their relative performance. * JavaScript engine variations: The JavaScript engine's implementation of sets and array includes can vary between engines (e.g., SpiderMonkey vs. V8), which could impact results. **Alternative Approaches** Other approaches to perform set membership checks include: 1. Using a library like `fast-set`: A lightweight, optimized set data structure for faster lookups. 2. Creating an object with the values as keys and using the object's `hasOwnProperty()` method: While not as efficient as sets, this approach can be useful when working with specific data structures or libraries that don't support sets. Keep in mind that these alternatives might introduce additional overhead, such as library dependencies or more complex implementations.
Related benchmarks:
Set Has vs Array Includes
Set array expansion Part 2
Set array expansion Part 3
set vs array includestratsatsrats
new set vs array includes
Comments
Confirm delete:
Do you really want to delete benchmark?