Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set replace
(version: 0)
Comparing performance of:
Clear vs New
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set(); var array = new Array(5000).fill().map((_, i) => i); for (let i = 0; i < array.length; i++) { set.add(i); }
Tests:
Clear
set.clear() for (let i = 0; i < array.length; i++) { set.add(i); }
New
set = new Set(array)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Clear
New
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Clear
655.8 Ops/sec
New
3904.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of creating, clearing, and adding elements to a Set data structure in JavaScript. A Set is an unordered collection of unique values, which makes it ideal for checking membership and removing duplicates. **Script Preparation Code** The script preparation code initializes two variables: * `set`: a new Set object * `array`: an array of 5000 integers, generated using the `Array` constructor with the `fill()` method followed by the `map()` method to generate unique values **Html Preparation Code** There is no HTML preparation code specified in this benchmark. **Individual Test Cases** The benchmark consists of two test cases: 1. **Clear**: This test case creates a new Set object and then clears it using the `clear()` method. The script then adds all elements from the original array to the cleared Set. 2. **New**: This test case simply assigns the original array to a new Set object without clearing or adding any elements. **Library Used** The Set data structure is implemented in JavaScript, which is a built-in library and does not require an additional library to be loaded. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. It only relies on standard JavaScript concepts such as arrays, Sets, loops, and assignment operators. **Options Compared** The two test cases compare the performance of: * Creating a new Set object from an array (Test Case "New") * Clearing the Set object before adding all elements from the original array (Test Case "Clear") **Pros and Cons** Here are some pros and cons for each approach: **Creating a new Set object from an array (Test Case "New")** Pros: * Faster initialization time, as it doesn't require clearing or updating an existing Set * May be more memory-efficient, depending on the size of the input array Cons: * Requires creating an additional temporary object to store the array elements * May incur additional overhead for copying or referencing the array elements **Clearing and adding elements to a Set (Test Case "Clear")** Pros: * Reuses the existing Set object, avoiding unnecessary memory allocations * May be more cache-friendly, as it allows the browser to optimize caching behavior Cons: * Requires clearing the Set object before adding new elements, which can incur additional overhead * May result in slower performance due to the overhead of clearing and updating the Set **Other Alternatives** Other alternatives for testing similar use cases might include: * Using a different data structure, such as an array or a linked list * Testing the performance of other operations, such as searching or removing elements from the Set * Comparing the performance of different JavaScript engines or browsers However, these alternatives would require significant changes to the benchmark code and may not accurately represent the specific use case being tested.
Related benchmarks:
for vs foreach vs map 2
fill vs map
Create New Array & Fill it
Array fill method vs for loop vs map
Comments
Confirm delete:
Do you really want to delete benchmark?