Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set() has vs add
(version: 1)
To figure out if it's even worth checking via .has or just spamming .add repeatedly.
Comparing performance of:
add vs has
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const set = new Set()
Tests:
add
set.add(1)
has
if (!set.has(1)) set.add(1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
add
has
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
add
108968120.0 Ops/sec
has
114322344.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
### Benchmark Overview The benchmark titled "Set() has vs add" is designed to analyze the performance of two different approaches for adding an item to a JavaScript `Set`. The purpose is to determine whether using the `Set.prototype.has` method to check for the existence of an item before adding it (using `Set.prototype.add`) is more efficient compared to indiscriminately adding items to the set. ### Options Compared 1. **Using `.add()`:** - **Test Case**: `set.add(1)` - This test simply adds an item `1` to the `Set` without checking if it already exists. 2. **Using `.has()` followed by `.add()`:** - **Test Case**: `if (!set.has(1)) set.add(1)` - This test checks if the item `1` exists in the `Set` first. Only if it does not exist, the item is added. ### Performance Results From the latest benchmark results, we have the following execution speeds for both methods: - **Test Name "has"**: 147,970,352 executions per second - **Test Name "add"**: 140,161,424 executions per second ### Pros and Cons of Each Approach 1. **Direct `.add()` Approach** - **Pros**: - Simplicity: The implementation is straightforward and concise. - Slightly better performance based on the results, as the method did execute more times per second in this benchmark. - **Cons**: - Potential duplicate entries: This approach does not prevent adding duplicates if it is run multiple times with the same value. 2. **Conditional `.has()` before `.add()` Approach** - **Pros**: - Prevents Duplicates: This method ensures that duplicates are not added, which can be significant in scenarios where integrity of the set is required. - **Cons**: - Additional overhead: The need to check for the existence of the item adds an extra function call, which may reduce overall execution speed, as indicated by the benchmark results. ### Other Considerations 1. **Use Case**: The choice between these two approaches may depend on specific use cases. If typical operations involve frequent attempts to add existing values, the conditional approach may be warranted despite the performance trade-off. 2. **Set Characteristics**: JavaScript `Set` objects are collections of values where each value must be unique. Understanding this allows developers to decide whether or not to use a conditional check based on the expected number of duplicates. ### Alternatives - **Using an Array**: An alternative for storing unique values could be an array combined with a manual check for duplicates using the `Array.prototype.includes()` method. However, this would not perform as well as a `Set` when dealing with large datasets. - **Using a Map**: If additional metadata associated with each value is necessary, a `Map` could be employed, but it varies from the simplicity of using a `Set` for unique values. - **Data Structures Libraries**: There are libraries such as Immutable.js or lodash that can provide enhanced structures with methods for ensuring uniqueness or managing collections efficiently, although this could introduce additional complexity and depend on external libraries. In conclusion, the choice between using `.has()` or `.add()` directly should be determined by the specific needs of the application, balancing performance against the necessity to maintain unique values in the dataset.
Related benchmarks:
new Set([x]).has vs [x].includes
array.includes vs set.has for small n
has vs includes
has vs includess
Const vs Has
array.includes vs. set.has on the fly
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
incl/set
Comments
Confirm delete:
Do you really want to delete benchmark?