Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set tests
(version: 0)
Comparing performance of:
Spread initialisation vs Foreach mutation
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Spread initialisation
let existing = new Set([1,2,3,4,5,6,7,9,10]) const newRecords = [{ id: 2 }, { id: null }, { id: 6 }, { id: 8 }, { id: 10 }] existing = new Set(...[existing, ...newRecords.map(({ id }) => id)])
Foreach mutation
let existing = new Set([1,2,3,4,5,6,7,9,10]) const newRecords = [{ id: 2 }, { id: null }, { id: 6 }, { id: 8 }, { id: 10 }] newRecords.forEach(r => { if (r.id) { existing.add(r.id) } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread initialisation
Foreach mutation
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):
Let's break down what's being tested in this JavaScript microbenchmark. **Benchmark Definition** The benchmark definition is the set of instructions that will be executed repeatedly to measure performance. In this case, there are two tests: 1. **Spread Initialization**: This test creates a new Set instance from an existing array and then adds elements to it using the spread operator (`...`). The initial array contains elements `[1,2,3,4,5,6,7,9,10]`, and then additional objects with `id` properties are added to the set. 2. **Foreach Mutation**: This test creates a new Set instance from an existing array and then iterates over it using `forEach`. For each iteration, if the `id` property is not null or undefined, the element is added to the set. **Options Compared** The two tests are comparing different approaches for adding elements to a Set instance: 1. **Spread Initialization**: This method uses the spread operator (`...`) to create a new array of elements and then adds it to the Set instance. 2. **Foreach Mutation**: This method iterates over an existing array using `forEach` and adds each element individually to the Set instance. **Pros and Cons** Here are some pros and cons for each approach: * **Spread Initialization**: + Pros: Faster, more concise, and easier to read. + Cons: May have higher overhead due to creating a new array. * **Foreach Mutation**: + Pros: More control over the iteration process, can be used with arrays of any size. + Cons: Slower, more verbose, and may lead to unnecessary iterations. **Library** There is no library explicitly mentioned in this benchmark. However, it's worth noting that Set instances are a built-in JavaScript data structure, so no additional libraries need to be imported or referenced. **Special JS Features/Syntax** The spread operator (`...`) is a modern JavaScript feature introduced in ES6 (ECMAScript 2015). It allows for creating new arrays from existing ones using the spread syntax. The `forEach` method is also a built-in JavaScript method that iterates over an array or any other iterable. **Other Alternatives** If these two approaches are not sufficient, alternative methods for adding elements to a Set instance include: * Using the `Array.prototype.forEach.call()` method to iterate over an existing array. * Using the `for` loop to iterate over an existing array and add each element individually. * Using a library like Lodash that provides more advanced iteration and set manipulation utilities. In summary, this benchmark is testing two different approaches for adding elements to a Set instance: spread initialization and foreach mutation. The spread initialization method is faster but may have higher overhead, while the foreach mutation method provides more control over the iteration process but is slower and more verbose.
Related benchmarks:
Create Set vs loop
loop over Set conditionally vs non-conditionally
set has vs list includes
new Set vs set.clear()
incl/set
Comments
Confirm delete:
Do you really want to delete benchmark?