Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes large
(version: 0)
Comparing performance of:
includes vs lookup
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; for(let i = 0; i < 100000; i++){ a.push(i); } var b = new Set(a)
Tests:
includes
return a.includes(50000)
lookup
return b.has(50000)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
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 what's being tested in this JavaScript microbenchmark. **Overview** The benchmark is designed to compare the performance of two approaches: `Set.has()` and `array.includes()`. Specifically, it tests whether searching for an element (in this case, 50,000) in a large set (`b`) or an array (`a`) is faster. **Options Compared** Two options are being compared: 1. **`Set.has()`**: This method checks if an element exists in a `Set` object. 2. **`array.includes()`**: This method searches for the presence of a value in an array. **Pros and Cons** **`Set.has()`**: Pros: * Generally faster than `array.includes()` because it uses a hash table data structure, which provides constant-time performance (O(1)). * Can be more efficient when dealing with large sets, as it avoids the overhead of iterating through the entire array. Cons: * Limited use cases: `Set.has()` is designed for searching for exact matches within a set. If you need to perform range queries or other operations on the set, this method may not be suitable. * Requires JavaScript objects (i.e., sets) to work efficiently. **`array.includes()`**: Pros: * More flexible and widely supported: can be used with any type of array, including arrays with duplicate elements. * Can be used for range queries or other operations on the array. Cons: * Slower than `Set.has()`: needs to iterate through the entire array to find the element, resulting in linear-time complexity (O(n)). * Less efficient when dealing with large arrays: has higher overhead compared to using a hash table data structure. **Other Considerations** * The benchmark uses a large array (`a`) with 100,000 elements and a `Set` object (`b`) initialized from this array. This allows for a fair comparison of the two methods. * The test case doesn't consider edge cases like empty sets or arrays, which could affect performance. **Library Used** In this benchmark, no specific library is required or utilized beyond the built-in JavaScript `Set` and `array.includes()` methods. **Special JS Features/Syntax** None are explicitly mentioned in the provided code snippets. However, it's worth noting that some older versions of JavaScript (e.g., ECMAScript 3) didn't support `Set` objects, while more modern browsers support them. **Alternatives** For searching for elements in a set or array, other approaches include: * Using a library like Lodash's `contains()` function * Implementing your own binary search algorithm * Utilizing data structures like a balanced binary search tree Keep in mind that the best approach depends on the specific use case and requirements.
Related benchmarks:
set.has vs. array.includes large 1
set.has vs. array.includes (1 million entries)
set vs array iteration 100k elements
set vs array iteration new new
Comments
Confirm delete:
Do you really want to delete benchmark?