Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (large array)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; for (var i = 0;i<100000;i++) { a[i] = 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 is being tested in the provided benchmark. **Benchmark Definition** The benchmark is designed to compare the performance of two different approaches for checking if an element exists within an array or a Set data structure: 1. `array.includes()`: This method checks if an element with the specified value (in this case, 50000) exists within the array. 2. `Set.has()`: This method checks if an element with the specified value (in this case, 50000) exists within the Set. **Options Compared** The benchmark is comparing two options: 1. **Array.includes()**: A method that searches for a specific element in an array by iterating through each element until it finds a match or reaches the end of the array. 2. **Set.has()**: A method that checks if an element exists within a Set data structure, which uses a hash table internally to store unique values. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: **Array.includes():** Pros: * Widely supported across different browsers and environments. * Can be used with arrays of arbitrary types (not just numbers). Cons: * Has a linear search complexity, which can lead to poor performance for large datasets. * May not be as efficient as other data structures like Sets. **Set.has():** Pros: * Provides fast lookups with an average time complexity of O(1), making it suitable for large datasets. * Eliminates duplicates automatically, reducing memory usage. Cons: * Requires a Set data structure to be created and populated beforehand. * May not be as widely supported across older browsers or environments. **Library Used** In this benchmark, the `Set` library is used. A Set is a data structure that stores unique values in a hash table, allowing for fast lookups and efficient memory usage. The `Set.has()` method is a part of the modern JavaScript API, introduced in ECMAScript 2015 (ES6). **Special JS Feature or Syntax** None mentioned in this benchmark. **Other Alternatives** If you're looking for alternative approaches to checking if an element exists within an array or Set, here are a few options: 1. **Array.prototype.indexOf()**: Similar to `array.includes()`, but returns the index of the first occurrence instead of a boolean value. 2. **Array.prototype.findIndex()**: A more modern alternative to `indexOf()` that also returns the index of the first occurrence, if found. 3. **JSON.stringify() + includes()**: An indirect approach using JSON stringification to create an array with a single element and then checking for its existence using `includes()`. Keep in mind that these alternatives may have different performance characteristics or trade-offs, depending on your specific use case.
Related benchmarks:
Includes (array) vs Has (Set)
set vs array find if exists
set vs array iteration 100k elements
set vs array iteration new new
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?