Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (big)
(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 max = 10000 var a = [...Array(max).keys()] var b = new Set(a) var c = Math.floor(Math.random() * max)
Tests:
Array.includes
return a.includes(c)
Set.has
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.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):
I'd be happy to explain the benchmark being tested on MeasureThat.net. **What is being tested?** The provided benchmark tests two approaches for checking if an element exists in a large array: `array.includes()` and `Set.has()`. Specifically, it checks how fast each approach can iterate over the elements of a set (created from an array) to find out if a randomly generated number (`c`) is present. **Options compared** Two options are being compared: 1. **Array.includes()**: This method checks if an element exists in the array by iterating over its indices and checking for a match. 2. **Set.has()**: This method uses a hash table to store elements of the set, allowing for fast lookup and existence checks. **Pros and Cons** - **Array.includes():** + Pros: widely supported and well-maintained across browsers and platforms. + Cons: - Can be slow for large arrays due to iteration over indices. - May have performance issues if the array is sparse or has many duplicate values. - **Set.has():** + Pros: - Fast lookup times due to hash table implementation. - Efficient even with a large number of elements in the set. + Cons: - Not all browsers and platforms support Sets or have efficient implementations. - Requires creating a Set object, which may incur additional overhead. **Library used** The `Set` data structure is being used here. A Set is a collection of unique values, ordered or unordered, that cannot contain duplicate elements. In JavaScript, the `Set` constructor takes an iterable (such as an array) and creates a set from its elements. **Special JS feature or syntax** This benchmark doesn't rely on any special JavaScript features or syntax. However, note that the use of the spread operator (`...Array(max).keys()`) to create an array from an iterable is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). **Other alternatives** If you needed to implement either `array.includes()` or `Set.has()` manually without using these methods, alternative approaches would be: - For `array.includes()`: + Manually iterating over the array's indices and checking for a match. - For `Set.has()`: + Using a custom data structure like an associative array (map) to store elements. In contrast to `Set.has()`, manually implementing such a lookup system would likely be slower, especially for large sets due to the overhead of managing a custom data structure. MeasureThat.net's benchmark is designed to compare these common JavaScript methods' performance in real-world scenarios, providing valuable insights into how different approaches can optimize your code's execution speed.
Related benchmarks:
set.has vs. array.includes Performance 2
set.has vs. array.includes Perf Random
set.has vs. array.includes Perf Random 1
set.has vs. array.includes (100000)
Comments
Confirm delete:
Do you really want to delete benchmark?