Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set add vs from array
(version: 0)
Set add vs from array
Comparing performance of:
add vs from array
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var n = 1000; var keys = [...Array(n)].map((_) => Math.floor(Math.random() * n));
Tests:
add
const keySetA = new Set(); for (const k of keys) { keySetA.add(k); }
from array
const acc = []; for (const k of keys) { acc.push(k); } const keySetB = new Set(acc);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
add
from array
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of JavaScript sets versus arrays is a fundamental benchmarking test, and understanding what's being tested can help us analyze the results. **What is being tested?** The benchmark compares two approaches: 1. **Set add**: This approach uses a `Set` object to store unique values from an array. The test measures how quickly adding elements to the set performs compared to... 2. **Array push and Set conversion**: This approach first creates an array with random elements, pushes those elements onto the array, and then converts the array to a `Set` using the `Set()` constructor. **Options being compared** In essence, the benchmark is testing two approaches: * Add elements directly to a `Set` object (set add) * Push elements onto an array first, then convert the array to a `Set` **Pros and Cons of each approach:** 1. **Set add** * Pros: + More efficient for adding unique values since it uses a data structure optimized for fast lookups. + Can be faster when working with large datasets. * Cons: + Requires a separate data structure (the `Set`) to store the elements, which can lead to additional memory usage and garbage collection overhead. 2. **Array push and Set conversion** * Pros: + Less memory overhead since it only uses an array and doesn't require an additional data structure for storing unique values. * Cons: + Slower due to the overhead of pushing elements onto an array, which is a slower operation than adding elements directly to a `Set`. **Special considerations** The benchmark doesn't explicitly mention any special JavaScript features or syntax. However, it's worth noting that this benchmark assumes modern JavaScript environments (ECMAScript 2020 and later) since it uses the `Set()` constructor. **Alternative approaches:** While not explicitly mentioned in the benchmark definition, other approaches could be explored: * Using `Map` instead of a `Set` for storing unique values. * Using a more efficient data structure like a `PriorityQueue` or a binary search tree (BST) for adding elements to. * Optimizing the `Set()` constructor by using its `init` method with an initial size. Keep in mind that these alternatives might not be directly comparable to the original set add and array push approaches, as they may have different performance characteristics and use cases.
Related benchmarks:
Fill array with random integers
Map vs Array vs Object vs Set add item speed in 50000 iters 2
.at vs [x]
at 500 vs [500]
Object vs Map lookup: random integer key
Comments
Confirm delete:
Do you really want to delete benchmark?