Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique elements of two arrays
(version: 0)
Comparing performance of:
Set + spread vs Set + Array.from vs Concat + spread vs lastIndexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array1 = []; for (let i = 0; i < 100000; i++) { array1.push(Math.floor((Math.random() * 10) + 1)); } var array2 = []; for (let i = 0; i < 100000; i++) { array2.push(Math.floor((Math.random() * 10) + 1)); }
Tests:
Set + spread
const uniqueValues = (arr1, arr2) => { const values = new Set(arr1) for(const value of arr2) { values.add(value) } return [...values] } uniqueValues(array1, array2)
Set + Array.from
const uniqueValues = (arr1, arr2) => { const values = new Set(arr1) for(const value of arr2) { values.add(value) } return Array.from(values) } uniqueValues(array1, array2)
Concat + spread
const uniqueValues = (arr1, arr2) => { return [...new Set(arr1.concat(arr2))] for(const value of arr2) { values.add(value) } return [...values] } uniqueValues(array1, array2)
lastIndexOf
const uniqueValues = (arr1, arr2) => { const array = arr1.concat(arr2) return array.filter((item, index) => array.lastIndexOf(item) != index); } uniqueValues(array1, array2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Set + spread
Set + Array.from
Concat + spread
lastIndexOf
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 the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark measures the performance of different approaches to find unique elements in two arrays. The test creates two large arrays (`array1` and `array2`) filled with random integers, and then finds the unique values by intersecting or combining these arrays using various methods. **Options Compared** Four different approaches are compared: 1. **Set + spread**: Uses a Set to store unique elements from the first array, then iterates over the second array and adds its elements to the Set. 2. **Set + Array.from**: Similar to the previous approach, but instead of using a Set, it uses `Array.from()` to create an array from the Set. 3. **Concat + spread**: Combines both arrays using `concat()`, then finds unique elements by spreading the resulting array and filtering out duplicates. 4. **lastIndexOf**: Uses the `lastIndexOf()` method to find the index of each element in the first array, and then filters out elements that are not at their expected index. **Pros and Cons** 1. **Set + spread**: * Pros: Efficiently finds unique elements with minimal overhead, as Sets have an average time complexity of O(1) for insertions. * Cons: May not perform well if the Set needs to store a large number of elements from the first array. 2. **Set + Array.from**: * Pros: Similar to the previous approach, but avoids creating a temporary array with `concat()`. * Cons: Still uses an extra operation to convert the Set to an array. 3. **Concat + spread**: * Pros: Can be faster than the previous approaches if the combined array is large and has many duplicates. * Cons: Has higher overhead due to creating a temporary array with `concat()`. 4. **lastIndexOf**: * Pros: Simple implementation, as it relies on built-in methods. * Cons: Has high time complexity (O(n^2)) due to the use of `lastIndexOf()` for each element. **Library and Special Features** The benchmark uses the following libraries: 1. **Set**: A built-in JavaScript object that stores unique values in an efficient manner. 2. **Array.from()**: A built-in JavaScript method that creates a new array from an iterable object. No special JavaScript features or syntax are used beyond what is typically available in modern browsers. **Other Alternatives** Some alternative approaches could be considered: 1. **Sort and Prune**: Sort both arrays, then iterate over the sorted array to find unique elements. 2. **Hybrid Approach**: Use a combination of Set-based methods (e.g., `Set + spread`) and other methods (e.g., `lastIndexOf`) for better performance. Keep in mind that the best approach may depend on specific use cases or requirements, such as memory constraints, cache efficiency, or readability considerations. Feel free to ask follow-up questions if you'd like me to elaborate on any of these points!
Related benchmarks:
Fill array with random integers
Preinitialized array size vs Push operations to an empty one.
Lodash.js vs Native Remove Duplicates
Array push or set
Comments
Confirm delete:
Do you really want to delete benchmark?