Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Small n set vs array
(version: 0)
Comparing performance of:
array.includes vs set.has
Created:
3 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:
array.includes
return a.includes(3)
set.has
return b.has(3)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.includes
set.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:134.0) Gecko/20100101 Firefox/134.0
Browser/OS:
Firefox 134 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.includes
72342520.0 Ops/sec
set.has
1617207936.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare two approaches for checking if an element exists in a data structure: 1. Using an array (`includes` method) 2. Using a Set (`has` method) **Script Preparation Code** The script preparation code creates two data structures: `a` (an array) and `b` (a Set, initialized with the elements of `a`). This sets up the test environment for both scenarios. ```javascript var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a); ``` **Options Compared** The benchmark compares two options: 1. **Array `includes` method**: This checks if the specified element (`3`) exists in the array `a`. 2. **Set `has` method**: This checks if the specified element (`3`) exists in the Set `b`. **Pros and Cons of Each Approach:** **Array `includes` method:** Pros: * Wide support across browsers * Easy to implement Cons: * May involve linear search, leading to O(n) time complexity * Can be slower for large datasets **Set `has` method:** Pros: * Fast lookups (O(1) average time complexity) * Efficient for large datasets Cons: * Requires a Set data structure, which may not be supported in all browsers * May require additional code to handle edge cases **Library Used:** The benchmark uses the built-in `Set` and array methods (`includes`, `has`) without any external libraries. **Special JS Features or Syntax:** None mentioned. Both scenarios use standard JavaScript features. **Alternatives:** Other alternatives for checking if an element exists in a data structure include: * Using a Map (if you need to store additional metadata with each value) * Implementing a custom lookup function using techniques like binary search * Using libraries like Lodash or Ramda, which provide optimized implementations of these operations For this specific benchmark, the Set `has` method is likely preferred due to its performance advantages. However, for more complex use cases or browsers that don't support Sets, the array `includes` method may still be a viable option.
Related benchmarks:
set vs array
set vs array iteration
Includes (array) vs Has (Set)
set vs array find if exists
Comments
Confirm delete:
Do you really want to delete benchmark?