Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes Performance 2
(version: 0)
set.has vs. array.includes Performance
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: 100000}, () => Math.floor(Math.random() * 100000)); 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 JavaScript benchmark on MeasureThat.net. **Benchmark Definition** The benchmark is defined as: `set.has vs. array.includes Performance 2` This means that two different approaches are being compared: 1. Using the `array.includes()` method 2. Using the `set.has()` method (specifically, on a Set object created from an array) **Options Compared** * `array.includes()`: This is a built-in JavaScript method that checks if a value exists in an array. * `set.has()`: This is a method on a Set object that checks if a value exists within the set. **Pros and Cons of Each Approach** 1. `array.includes()`: * Pros: Easy to use, widely supported, fast for small arrays. * Cons: Can be slow for large arrays (due to the need to search the entire array), may not be optimized for performance on certain browsers or hardware. 2. `set.has()`: This approach is more efficient than `array.includes()` because it only needs to check if the value exists in a subset of the data, rather than searching the entire array. However: * Pros: Fast for large datasets, can take advantage of indexing and caching on modern browsers and hardware. * Cons: May not be as intuitive or easy to use as `array.includes()`, requires an additional library (in this case, a Set object) to be created. **Library Used** In this benchmark, the Set object is used to create a set from an array. A Set object in JavaScript is a collection of unique values that cannot have duplicate entries. It's similar to an array, but with a few key differences: * Sets are unordered collections (unlike arrays). * Sets do not allow duplicate values. * Sets can be faster than arrays for certain operations. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax. However, the creation of the Set object is an important aspect of this approach. To create a Set object in JavaScript, you would typically use the `new Set()` constructor and pass an iterable (such as an array) to it. In this benchmark, the script preparation code creates an array from `{length: 100000}` using `Array.from()`, which returns an array of random numbers between 0 and 100000. **Other Alternatives** If you needed to compare the performance of different approaches to check if a value exists in an array or set, other alternatives could include: * Using the `indexOf()` method on an array (although this is generally slower than `includes()`). * Implementing your own custom search algorithm. * Using a library like Lodash or Ramda that provides optimized implementations of these operations. In terms of using alternative data structures, you might also consider using arrays with indexes or other specialized data structures for specific use cases. However, in general, the Set object and `array.includes()` approach are among the most common and efficient ways to check if a value exists in an array.
Related benchmarks:
Set.has vs. Array.includes
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?