Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs [...new Set]
(version: 0)
Comparing performance of:
Set vs Array
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Tests:
Set
var l = [...new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])]; return l;
Array
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
19 hours ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
3648056.5 Ops/sec
Array
8912620.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to remove duplicates from an array: using a `Set` object and using Lodash's `uniq()` function. **Options Compared** There are two options being compared: 1. **Using a Set Object**: This approach uses the built-in `Set` object in JavaScript, which automatically removes duplicates by only storing unique values. 2. **Using Lodash's uniq() Function**: This approach uses the `uniq()` function from the Lodash library, which also removes duplicates from an array. **Pros and Cons** **Using a Set Object:** Pros: * Fast and efficient, as it leverages the optimized implementation of the `Set` object. * Simple to implement and understand. * Eliminates duplicates in a single pass through the data. Cons: * May not work as expected if the input array contains non-numeric or non-string values that can be hashed (e.g., objects, functions). * Does not preserve the original order of elements. **Using Lodash's uniq() Function:** Pros: * Preserves the original order of elements. * Works with any type of data that can be compared using the `===` operator. * Can handle edge cases like NaN and undefined values. Cons: * Slower than using a `Set` object, as it iterates over the array and checks for duplicates using the `===` operator. * Requires an external library (Lodash), which may add overhead and dependencies. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of functional programming helpers. In this benchmark, the `uniq()` function is used to remove duplicates from an array. The `uniq()` function takes an optional second argument to specify the comparison function; in this case, no comparison function is provided, so it defaults to the default comparison function. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. However, it's worth noting that the use of Lodash and the `Set` object both rely on modern JavaScript features like closures and prototypal inheritance. **Other Alternatives** If you didn't have access to Lodash, you could implement a custom implementation for removing duplicates from an array using the following approach: ```javascript function removeDuplicates(arr) { const seen = {}; return arr.filter(x => seen[x] === undefined ? seen[x] = true : false); } ``` This implementation uses a simple object (`seen`) to keep track of unique values and iterates over the input array using the `filter()` method. Alternatively, you could use a more advanced algorithm like the "counting sort" or "bucket sort" approach, which can be more efficient for large datasets but are also more complex to implement.
Related benchmarks:
lodash uniq vs vanilla set - final
lodash uniq vs native uniq
Lodash uniq vs Set to unique array
Lodash - uniq
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?