Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes on big arrays
(version: 0)
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 1000 }, (_, i) => i + 1) var b = new Set(a)
Tests:
includes
return a.includes(9)
lookup
return b.has(9)
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 the provided benchmark definition and test cases. **Benchmark Overview** The benchmark measures the performance difference between using `array.includes()` versus `Set.has()` on large arrays. The benchmark creates two arrays of size 1000, one with sequential integers and another without any values, and then converts these arrays to a Set data structure. The test then checks whether a specific value (9) is present in each array/set. **Options Compared** The benchmark compares the performance of two approaches: 1. `array.includes()`: This method searches for a specified element within an array. 2. `Set.has()`: This method checks if a specific element exists within a Set data structure. **Pros and Cons** ### Array includes() Pros: * Widely supported in modern browsers * Easy to understand and implement * Typically faster than using Set, especially for small arrays Cons: * Not optimized for large arrays or datasets * May be slower than Set for certain use cases (e.g., when the array is sparse) ### Set has() Pros: * Optimized for searching in large datasets (e.g., sets of unique values) * Can take advantage of caching and optimized data structures Cons: * Less widely supported, especially in older browsers * May require more complex implementation and setup * Requires the value to be present in the set for a faster lookup **Library Used** In this benchmark, `Array.from()` is used to create large arrays, while `Set` is used to create sets from those arrays. These functions are part of the ECMAScript standard library. **Special JS Feature or Syntax** None mentioned, but it's worth noting that `Array.from()` and `Set` were introduced in ECMAScript 2015 (ES6). **Other Considerations** When deciding between `array.includes()` and `Set.has()`, consider the following factors: * Data size: For very large datasets, `Set.has()` may be faster due to its optimized data structure. * Data uniqueness: If the dataset is expected to contain unique values, using a Set can improve performance. * Browser support: If supporting older browsers, you might prefer `array.includes()`. * Code readability and maintainability: Choose the approach that best fits your code's requirements and readability needs. **Alternatives** Other alternatives for searching arrays or sets include: * Using `Array.prototype.indexOf()` (alternative to `array.includes()`) * Implementing custom lookup algorithms using bitwise operations * Using libraries like Lodash or Underscore.js, which provide optimized array and set methods Keep in mind that the choice of approach depends on your specific use case, performance requirements, and coding style.
Related benchmarks:
set.has vs. array.includes on big arrays s
Includes (array) vs Has (Set)
array.includes vs set.has for small-ish n
array.includes vs set.has for medium n
set.has vs. array.includes (find 999,999 in 1,000,000)
Comments
Confirm delete:
Do you really want to delete benchmark?