Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create new set vs clear existing set
(version: 1)
Comparing performance of:
new set vs clear set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
let permSet = new Set() for (let i = 0; i < 100; i++) { permSet.add(Math.floor(Math.random() * 100000)); }
Tests:
new set
permSet = new Set()
clear set
permSet.clear()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new set
clear set
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new set
4467840.0 Ops/sec
clear set
10760818.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described in the provided JSON from MeasureThat.net evaluates the performance of two different operations on JavaScript's `Set` object: creating a new set and clearing an existing set. ### Options Compared 1. **Creating a New Set (`permSet = new Set()`)**: - **Test Name**: new set - This operation tests the time it takes to instantiate a new `Set`, which involves allocating memory and initializing the structure to hold no elements initially. 2. **Clearing an Existing Set (`permSet.clear()`)**: - **Test Name**: clear set - This operation measures how quickly the `clear()` method can remove all elements from an existing `Set`, effectively resetting its state while retaining its allocated memory. ### Performance Results Analyzing the results, we see: - The `clear set` operation has a performance of approximately **10,760,818 executions per second**. - The `new set` operation shows a significantly lower performance of around **4,467,840 executions per second**. ### Pros and Cons **Creating a New Set**: - **Pros**: - Conceptually simple and straightforward. Each new instance is independent and has its own memory allocation. - **Cons**: - More computational overhead due to the need to allocate memory and potentially invoke garbage collection for previous objects. **Clearing an Existing Set**: - **Pros**: - Typically faster in scenarios where the same `Set` is reused multiple times, as it doesn't require memory reallocation. - Increased efficiency when dealing with frequent resets of a `Set`, as the underlying memory remains allocated. - **Cons**: - The `Set` remains in memory but with no contents, which could lead to memory being consumed unnecessarily if not reused appropriately. ### Other Considerations When deciding between creating a new `Set` versus clearing an existing one, developers should consider the context of their application: - If sets are frequently created and destroyed, the overhead of the new instance creation can incur performance penalties. In this case, clearing an existing `Set` can be beneficial. - If memory usage is a concern, especially in long-running applications, reusing `Set` instances can contribute to lower memory footprint compared to instantiating new ones repeatedly. ### Alternative Approaches 1. **Array**: - An alternative to `Set` could be an `Array`. While `Array` allows for similar operations, it lacks the same guarantee of unique elements. This can lead to duplicate entries requiring additional logic to manage uniqueness, which could impact performance. 2. **Using Objects as Maps**: - Another approach could be using an object to store unique keys where the properties themselves act like unique entries. However, this is less straightforward compared to the `Set` API, which is designed specifically for the purpose of storing unique values. In conclusion, for performance-sensitive applications where `Set` operations are critical, especially for frequent resets, utilizing the `clear()` method on existing sets is generally more efficient than creating new instances. Understanding how these operations impact memory and performance can guide developers in choosing the appropriate data structure and method for their applications.
Related benchmarks:
set.add vs array.push
New set vs set clear
Set vs Object iteration
New set vs set clear 2
add vs object
new Set vs set.clear()
Set array expansion
Set array expansion Part 2
New set vs set clear (no const)
Comments
Confirm delete:
Do you really want to delete benchmark?