Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immutable.Set Union vs Constructing a new plain JS Set - small
(version: 0)
Comparing performance of:
Immutable.Set union vs Convert JS Set to array, construct new Set vs Array.from, construct new Set
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/immutability-helper@2.7.0/index.min.js"></script>
Tests:
Immutable.Set union
const set1_Immutable = new Immutable.Set(Array.from(Array(100).keys())); const set2_Immutable = new Immutable.Set(Array.from(Array(20).keys())); const result = set1_Immutable.union(set2_Immutable)
Convert JS Set to array, construct new Set
const set1_JS = new Set(Array.from(Array(100).keys())); const set2_JS = new Set(Array.from(Array(20).keys())); const result = new Set([...set1_JS, ...set2_JS])
Array.from, construct new Set
const set1_JS = new Set(Array.from(Array(100).keys())); const set2_JS = new Set(Array.from(Array(20).keys())); const result = new Set(Array.from(set1_JS).concat(Array.from(set2_JS)))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Immutable.Set union
Convert JS Set to array, construct new Set
Array.from, construct new Set
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 dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark compares three approaches for performing set unions in JavaScript: 1. Using the Immutable.js library 2. Converting the sets to arrays and constructing new sets using the spread operator (`...`) 3. Using `Array.from()` to convert one of the sets to an array, then concatenating it with the other set **Immutable.js Library** The Immutable.js library is a JavaScript library for functional programming that provides immutable data structures, such as sets. In this benchmark, the Immutable.js library is used to create two sets (`set1_Immutable` and `set2_Immutable`) and perform an union operation on them. Pros of using Immutable.js: * Provides a predictable and deterministic way of performing set operations * Can help prevent common errors that can occur when working with mutable data structures Cons of using Immutable.js: * Adds extra overhead due to the creation of new, immutable data structures * May not be suitable for all use cases where performance is critical **Converting Sets to Arrays and Constructing New Sets** This approach involves converting one or both of the sets to arrays using `Array.from()`, then using the spread operator (`...`) to concatenate the arrays and construct a new set. Pros of this approach: * Does not require any additional libraries * Can be optimized for performance by avoiding unnecessary memory allocations Cons of this approach: * May lead to slower performance compared to other approaches due to the overhead of array conversions * Requires manual error handling to ensure that the resulting set is correctly constructed **Array.from() and Concatenation** This approach involves using `Array.from()` to convert one of the sets to an array, then concatenating it with the other set using the `+` operator. Pros of this approach: * Does not require any additional libraries * Can be optimized for performance by avoiding unnecessary memory allocations Cons of this approach: * May lead to slower performance compared to other approaches due to the overhead of array conversions and concatenation * Requires manual error handling to ensure that the resulting set is correctly constructed **Other Considerations** * Memory allocation: The benchmark measures the number of executions per second, which can be affected by memory allocation. The Immutable.js library may perform better in terms of memory allocation compared to other approaches. * Cache locality: The performance difference between these approaches may also depend on cache locality, as the JavaScript engine optimizes access patterns for arrays. **Alternatives** Other alternatives for set operations include: * Using the `Set` class's built-in union method (`set1.set(set2)`) * Utilizing libraries like Lodash or Ramda, which provide a broader range of functional programming utilities, including set operations. * Implementing custom set operations using native JavaScript features, such as iterators and generators. These alternatives may offer different trade-offs in terms of performance, readability, and maintainability, depending on the specific use case.
Related benchmarks:
object spread vs immutable-js set (large)
immutable vs lodash vs ... 4
Spread operator vs Immutable.js performance for common use cases
Immutable.Set Union vs Constructing a new plain JS Set
Comments
Confirm delete:
Do you really want to delete benchmark?