Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes - including extra code for more fair comparison as typically you will have an array and not a set
(version: 0)
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // var b = new Set(a)
Tests:
includes
return a.includes(9)
lookup
var b = new Set(a) 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 what's being tested in the provided benchmark. **Benchmark Definition** The benchmark is comparing two approaches: 1. `includes`: Using the `array.includes()` method to check if an element exists in an array. 2. `lookup`: Using a `Set` data structure to store elements and then checking if an element exists using the `has()` method. **Options Compared** The two options being compared are: * Array-based approach (`includes`) vs. Set-based approach (`lookup`) **Pros and Cons of Each Approach** **Array-based approach (`includes`)** Pros: * Fast lookup time, as it uses a hash table to store element indices. * Efficient for large datasets. Cons: * Requires the array to be created first, which can lead to additional memory allocation overhead. * Can be slower for very small arrays (less than 10 elements) due to the overhead of creating the array. **Set-based approach (`lookup`)** Pros: * Fast lookup time, as it uses a hash table to store element values. * Efficient for large datasets and can handle any number of elements. * Can be faster than `includes()` for very small arrays (less than 10 elements). Cons: * Requires creating a new Set object, which can lead to additional memory allocation overhead. * May have slower startup time due to the creation of the Set object. **Other Considerations** The benchmark assumes that the array will always contain more than one element, as mentioned in the Script Preparation Code (`// var b = new Set(a)`). This is because the `includes()` method can return false for empty arrays or arrays with only one element, whereas `has()` requires a non-empty Set. **Library Used** In this benchmark, no libraries are used. However, note that if you were to use a library like Lodash, it might offer an optimized implementation of `includes()` called `lodash.isInclude()`, which could potentially change the results. **Special JS Feature or Syntax** There are no special JavaScript features or syntax being tested in this benchmark. The focus is on comparing the performance of two different approaches using standard language constructs (`array.includes()` and `Set.has()`).
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
set.has vs. array.includes v22
array.includes vs. set.has on the fly
Array includes vs Set.has
Comments
Confirm delete:
Do you really want to delete benchmark?