Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique array 3
(version: 0)
Comparing performance of:
array set unique vs array filter unique
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
existingIds = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; newIds = [5,18,21,22,23,24,25,26,27] filterUniqueValues = (id, index, array) => index === array.indexOf(id);
Tests:
array set unique
const resultingArray = Array.from(new Set(existingIds.concat(newIds)))
array filter unique
const resultingArray = existingIds.concat(newIds).filter(filterUniqueValues);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array set unique
array filter unique
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):
I'll break down the provided benchmark and its test cases to explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is for JavaScript microbenchmarks on an array of numbers. It tests two different approaches: using `Array.from` with a set, and filtering unique values from an existing array. **Script Preparation Code** The script preparation code defines two arrays: `existingIds` and `newIds`. The purpose of this code is to create an initial dataset for the benchmark. Here's what's happening: * `existingIds` is initialized with numbers 1-20. * `newIds` is initialized with a subset of numbers from `existingIds`: [5, 18, 21, 22, 23, 24, 25, 26, 27]. **Benchmark Test Cases** There are two test cases: ### Array Set Unique ```javascript const resultingArray = Array.from(new Set(existingIds.concat(newIds))); ``` This approach uses the `Set` constructor to create a set from the concatenated array (`existingIds + newIds`). The `Array.from()` method converts this set back into an array, which is stored in the `resultingArray` variable. **Pros:** * This approach is likely faster because it avoids iterating over the array multiple times. * It's a more straightforward way to remove duplicates from an array. **Cons:** * It can be less efficient than filtering the original array if there are many duplicate values. ### Array Filter Unique ```javascript const resultingArray = existingIds.concat(newIds).filter(filterUniqueValues); ``` This approach uses the `concat()` method to concatenate the two arrays, and then filters out duplicates using the `filter()` method with a custom callback function (`filterUniqueValues`). **Pros:** * It's more flexible than using an array set because it allows for more control over the filtering process. * It can be useful if you need to perform additional processing on each unique value. **Cons:** * This approach requires iterating over the array twice (once with `concat()` and once with `filter()`), which can be slower. * The custom callback function (`filterUniqueValues`) adds complexity to the benchmark. **Library: Set** The `Set` constructor is a built-in JavaScript library that creates an object data structure for storing unique values. It's used in both test cases to remove duplicates from the concatenated array. **Other Considerations** When evaluating these approaches, consider the following factors: * The size of the input arrays (`existingIds` and `newIds`). * The number of duplicate values in the arrays. * The performance characteristics of each approach on your specific use case. **Alternative Approaches** If you need to remove duplicates from an array but don't want to use `Set`, you can consider using other approaches: * Using a regular array with `indexOf()` and checking for negative indices (not recommended due to its slow performance). * Implementing a custom merge-and-check algorithm. * Utilizing a third-party library like Lodash's `uniq` function. However, these alternatives are generally less efficient than the `Set` approach or the filtering approach used in the benchmark.
Related benchmarks:
unique array 2
Unique values
Unique values big list
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?