Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
New set vs set clear (fixed)
(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:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new set
15422695.0 Ops/sec
clear set
43931356.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON focuses on comparing two different approaches to creating or modifying a JavaScript `Set`: creating a new set and clearing an existing one. ### Options Compared: 1. **Creating a New Set (`new set`)** - **Code**: `permSet = new Set()` - **Description**: This test measures the performance of instantiating a new `Set` object, which initializes an empty collection that can store unique values of any type (including objects). - **Pros**: - Results in a completely new instance, which may help in scenarios where the previous data is no longer needed and you want to start fresh. - May offer a cleaner approach in code, as it eliminates any residual data from the previous instance. - **Cons**: - Creating a new object incurs overhead for memory allocation and garbage collection, especially in high-frequency operations. - If used in a loop or frequently, may lead to decreased performance due to the repetitive creation and destruction of objects. 2. **Clearing an Existing Set (`clear set`)** - **Code**: `permSet.clear()` - **Description**: This test evaluates the performance of the `clear` method of a `Set`, which removes all elements from the existing set without deallocating the `Set` object itself. - **Pros**: - Faster than creating a new set since it reuses the existing object and simply resets its state. - Reduces memory allocation demands and garbage collection overhead, which can be beneficial in performance-critical applications. - **Cons**: - The internal structure still exists, meaning the memory footprint of the `Set` instance is still maintained even if it is empty. - Offers less clarity compared to creating a new instance, as the object remains the same even when cleared. ### Other Considerations: - **Performance Metrics**: The benchmark results show that the `clear set` operation yields a performance of approximately 47 million executions per second, whereas the `new set` operation is at about 19 million executions per second. This indicates that the `clear` method is significantly more efficient for cases where you need to reset a collection. - **Browser & Environment**: The tests were executed using Chrome (version 133) on a Linux desktop platform. Performance may vary across different environments and browsers due to variations in JavaScript engines and optimizations. ### Alternatives: Beyond the methods tested, alternative approaches could include: - **Using Arrays**: If duplicate elements are permissible, arrays could be an alternative to sets, although they lack the uniqueness constraint that sets provide. The performance characteristics would differ from sets based on operations like searching and inserting. - **Object as a Hash Table**: For cases where unique keys are needed, JavaScript objects can also be employed. However, they do not provide the built-in iteration and methods such as `clear`. - **WeakSet**: If you need to associate unique objects without preventing garbage collection of those objects, `WeakSet` can be used. This functionality can be useful in specific memory management scenarios, though its API differs. In summary, this benchmark highlights the performance differences between creating new instances and clearing existing objects in JavaScript. Software engineers should consider these implications based on their specific use cases and performance requirements.
Related benchmarks:
New set vs set clear
Set vs Object iteration
testtestasdadadadadsada
New set vs set clear 2
add vs object
new Set vs set.clear()
Set array expansion
New set vs set clear (no const)
Create new set vs clear existing set
Comments
Confirm delete:
Do you really want to delete benchmark?