Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set.has vs. Array.includes
(version: 0)
Comparing performance of:
Array vs Set
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const len = 100000; var a = Array.from({length: len}, (_, i) => i); var b = new Set(a); var c = Math.floor(Math.random() * len);
Tests:
Array
return a.includes(c)
Set
return b.has(c)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array
Set
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 dive into the world of JavaScript microbenchmarks and explore what's being tested on this specific benchmark. **Benchmark Overview** The benchmark measures the performance difference between using `Set.has()` versus `Array.includes()` to check if an element exists in a set or array, respectively. The test case creates two arrays and sets with 100,000 elements, then generates a random index `c` and checks if it exists in both data structures. **Options Compared** Two options are being compared: 1. **Set.has()**: This method uses a hash table to store the elements of the set. When you call `has(c)`, it checks if the element at index `c` is present in the set. 2. **Array.includes()**: This method searches for an element in the array by iterating through its indices and checking if the element matches the one being searched for. **Pros and Cons of Each Approach** 1. **Set.has()**: * Pros: + Faster lookups due to hash table's O(1) average time complexity. + Efficient when dealing with large datasets or frequent checks. * Cons: + Requires additional memory to store the set. + May be slower for small datasets or infrequent checks. 2. **Array.includes()**: * Pros: + No additional memory requirements. + Works well for small datasets or occasional searches. * Cons: + Slower lookups due to linear search's O(n) time complexity. + May be slower for large datasets or frequent checks. **Library and Purpose** The `Array.from()` method is used to create an array with a specified length, and the `Set` constructor creates a set from an array. Both are built-in JavaScript methods. **Special JS Feature/Syntax** There's no special feature or syntax being tested in this benchmark. **Other Considerations** * The benchmark uses `const len = 100000;`, which ensures that the dataset size remains constant across runs. * The random index `c` is generated using `Math.floor(Math.random() * len)`, ensuring a uniform distribution of values. * The test cases are designed to measure the performance difference between `Set.has()` and `Array.includes()`, providing a fair comparison. **Alternatives** If you'd like to explore alternative approaches, consider these options: 1. **Binary Search**: For large datasets, using binary search can significantly improve lookup times. 2. **Hash Table-like Data Structures**: Implementing custom hash table-like data structures, such as `Map` or `WeakSet`, could potentially outperform built-in sets and arrays. 3. **Cache-based Optimization**: Implementing cache mechanisms to store frequently accessed elements in arrays and sets could reduce lookup times. Keep in mind that these alternatives might add complexity and require additional development effort. The original benchmark provides a straightforward comparison between two widely used methods, making it an excellent starting point for exploring performance optimizations in JavaScript.
Related benchmarks:
Set.prototype.has() vs "in" operator
Set vs Objects vs Array 2
array vs set lookup
Lodash _.union vs the native Set() for multiple arrays
Comments
Confirm delete:
Do you really want to delete benchmark?