Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique values
(version: 0)
Comparing performance of:
Set vs Filter vs Reduce vs Lodash
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [ 1, 2, 2, 1, 3];
Tests:
Set
[...new Set(array)];
Filter
array.filter((item, index) => array.indexOf(item) === index);
Reduce
array.reduce((unique, item) => unique.includes(item) ? unique : [...unique, item], []);
Lodash
_.uniq(array)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Set
Filter
Reduce
Lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
9009732.0 Ops/sec
Filter
5748371.0 Ops/sec
Reduce
13698824.0 Ops/sec
Lodash
11298925.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of four different approaches for removing duplicates from an array: `Set`, `Filter`, `Reduce`, and `Lodash's uniq` function. **Script Preparation Code** The script preparation code defines the input array: ```javascript var array = [1, 2, 2, 1, 3]; ``` This array has duplicate values to simulate a scenario where duplicates need to be removed. **Html Preparation Code** Since there is no HTML preparation code provided, we can assume that it's not required for this benchmark or is already handled by the MeasureThat.net framework. **Individual Test Cases** Each test case has a `Benchmark Definition` property that defines how to create an array with unique values. There are four test cases: 1. **Set**: `[...new Set(array)]` This approach creates a new `Set` object from the input array, which automatically removes duplicates. 2. **Filter**: `array.filter((item, index) => array.indexOf(item) === index)` This approach uses the `filter()` method to create a new array with only unique elements. The callback function checks if each element is already present at its original index in the array. 3. **Reduce**: `array.reduce((unique, item) => unique.includes(item) ? unique : [...unique, item], [])` This approach uses the `reduce()` method to iterate over the input array and create a new array with unique elements. The accumulator variable `unique` is initialized as an empty array, and for each element, it checks if the element is already present in the accumulator using the `includes()` method. 4. **Lodash's uniq**: `_.uniq(array)` This approach uses Lodash's `uniq()` function to remove duplicate values from the input array. **Pros and Cons of Each Approach** 1. **Set**: * Pros: Fast, efficient, and concise. * Cons: Requires a modern JavaScript environment that supports Set objects (introduced in ECMAScript 2015). 2. **Filter**: * Pros: Simple, easy to understand, and works with older browsers. * Cons: Less efficient than the other approaches, as it creates a new array with filtered elements. 3. **Reduce**: * Pros: Flexible, can be used for more complex data processing tasks. * Cons: Less efficient than the Set approach, as it uses an accumulator variable and checks for duplicates using `includes()`. 4. **Lodash's uniq**: * Pros: Convenient, easy to use, and works with older browsers that support Lodash. * Cons: Requires a dependency on Lodash, which adds overhead. **Other Alternatives** 1. **Array.prototype.reduceRight()**: Similar to the `reduce()` method, but iterates over the array in reverse order. 2. **Array.prototype.filter()`: Removes elements from an array that do not meet a specified condition (similar to the Filter approach). 3. **Array.prototype.map()`: Creates a new array with transformed values, which can be used in combination with `filter()` or other methods. In conclusion, each approach has its strengths and weaknesses, and the choice of method depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Uniq Array - lodash uniq vs Array from Set
_.uniq vs Array.from(new Set())
Lodash unique vs Array.from(new Set)
lodash uniq vs deconstructed set
lodash uniq vs array.from set
Comments
Confirm delete:
Do you really want to delete benchmark?