Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (strings; set generation inline)
(version: 1)
Comparing performance of:
includes 1 vs includes 10 vs lookup 1 vs lookup 10
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
Tests:
includes 1
return a.includes('1')
includes 10
return a.includes('10')
lookup 1
return new Set(a).has('1')
lookup 10
return new Set(a).has('10')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes 1
includes 10
lookup 1
lookup 10
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes 1
99300736.0 Ops/sec
includes 10
62487556.0 Ops/sec
lookup 1
5672828.5 Ops/sec
lookup 10
5757537.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark outlined in the provided JSON measures and compares the performance of two different approaches to check for the existence of string elements in an array: using the `Array.includes()` method and using a `Set` with the `Set.has()` method. The benchmark code sets up an array of strings from '1' to '10', and it runs two types of tests: inclusion checks on the array and set lookups. ### Approaches Compared 1. **Array.includes()** - **Usage in Tests:** - `a.includes('1')` - Checks if the string '1' is included in the array. - `a.includes('10')` - Checks if the string '10' is included in the array. - **Performance (Executions Per Second):** - `includes 1`: 55,879,268.0 - `includes 10`: 47,879,072.0 2. **Set.has()** - **Usage in Tests:** - `new Set(a).has('1')` - Creates a Set from the array and checks if '1' exists in the Set. - `new Set(a).has('10')` - Creates a Set from the array and checks if '10' exists in the Set. - **Performance (Executions Per Second):** - `lookup 1`: 4,217,641.5 - `lookup 10`: 3,826,230.25 ### Pros and Cons of Each Approach #### Array.includes() - **Pros:** - Simplicity: The method is straightforward and easy to use. - No extra data structure overhead: You don't need to create a Set if you're checking an array that is already available. - **Cons:** - Performance: This method has a linear time complexity O(n), meaning in the worst case, it will check each item in the array until it finds a match or confirms absence. This can result in slower performance, especially if the array size increases. #### Set.has() - **Pros:** - Performance: Checking for existence in a Set is average O(1) time complexity, meaning it can check for items very quickly once the Set is created. - Ideal for frequent lookups: If you need to check many items frequently against a collection, creating a Set from an array initially can lead to faster lookups. - **Cons:** - Overhead: Creating a Set from the array incurs additional overhead, as it requires constructing a new data structure regardless of whether the checks are done multiple times. - Memory Usage: Using a Set adds extra memory usage due to storing the Set itself. ### Other Considerations When choosing between these two methods, it's important to consider the context: - If you're performing a single or a few checks on a small, fixed array, using `Array.includes()` is fine and less resource-intensive. - For larger datasets or when multiple lookups are necessary, using a `Set` can be worth the overhead. ### Alternatives to Consider 1. **Array.indexOf()**: An older method that can check for existence, but like `includes()`, it has a time complexity of O(n). 2. **Filtered Approach**: You could use `Array.filter()` or `Array.find()` but they may also have similar performance issues as `includes()` and less clarity for existence checks. 3. **Object Lookup**: In some scenarios, using an object as a map (where properties are keys) can emulate set-like behavior with constant time lookups. In conclusion, the benchmark provides valuable insights into the efficiency of string lookups in JavaScript, illustrating the trade-offs between simplicity and performance, particularly between `Array.includes()` and `Set.has()`. The choice between them should be guided by the specific requirements of the tasks being performed.
Related benchmarks:
set.has vs. array.includes - including extra code for more fair comparison as typically you will have an array and not a set
set.has vs. array.includesdd
set.has vs. array.includes (100 items)
set.has vs. array.includes 2333
set.has vs. array.includes (large: 100)
set.has vs. array.includes first element
set.has vs. array.includes (0)
array.includes vs. set.has on the fly
set.has vs. array.includes (strings)
Comments
Confirm delete:
Do you really want to delete benchmark?