Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set2
(version: 0)
Comparing performance of:
set vs uniq
Created:
7 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>
Script Preparation code:
var arr = [5,1,3,7,9,2,11]; var mySet = new Set(arr);
Tests:
set
mySet.add(12); mySet.add(12); mySet.add(5); mySet.add(17); mySet.add(1); mySet.add(11); return arr;
uniq
arr = _.uniq(arr.push(12)); arr = _.uniq(arr.push(12)); arr = _.uniq(arr.push(5)); arr = _.uniq(arr.push(17)); arr = _.uniq(arr.push(1)); arr = _.uniq(arr.push(12)); return arr;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
uniq
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 its test cases. **Benchmark Overview** The benchmark compares two JavaScript methods for removing duplicates from an array: `lodash uniq` (from the Lodash library) and the built-in `Set` object in JavaScript. **Options Compared** The two options are: 1. **Lodash uniq**: This method uses a similar approach to the built-in `Set` object, but with additional features like handling null and undefined values, and preserving the original order of elements. 2. **Built-in Set object**: This method uses an internal implementation that stores unique values in an array-like data structure. **Pros and Cons** 1. **Lodash uniq** * Pros: + Preserves the original order of elements + Handles null and undefined values * Cons: + May be slower due to the additional features 2. **Built-in Set object** * Pros: + Generally faster than Lodash uniq + More lightweight, as it doesn't require an external library * Cons: + Doesn't preserve the original order of elements + Throws an error if you try to add a null or undefined value **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, object creation, and more. The `uniq` function in particular is designed to remove duplicate values from an array while preserving the original order. In the benchmark, Lodash uniq is used to create a set-like data structure with unique values, allowing us to compare its performance with the built-in Set object. **Special JS Feature: Rest Parameter** The test cases use the rest parameter syntax (`arr.push(12)`) to dynamically add elements to an array. This feature allows you to pass a variable number of arguments to a function, which is useful for tasks like the benchmark where we need to add multiple values to an array. **Benchmark Preparation Code** The preparation code sets up the test data and environment: * `var arr = [5,1,3,7,9,2,11];`: Creates an initial array with duplicate values. * `var mySet = new Set(arr);`: Creates a Set object from the array, which is used as a baseline for Lodash uniq. **Individual Test Cases** The two test cases are: 1. **set**: This test case uses the built-in Set object to remove duplicates from the initial array and returns the resulting array. 2. **uniq**: This test case uses Lodash uniq to remove duplicates from the initial array and returns the resulting array. **Latest Benchmark Result** The latest benchmark result shows that the `uniq` test (using Lodash) performs slightly better than the `set` test (using the built-in Set object), with approximately 115,800 more executions per second. However, this difference is relatively small, suggesting that both methods are suitable for most use cases. **Other Alternatives** Some other alternatives to removing duplicates from an array in JavaScript include: * Using a filter function with the `every()` method: `arr.filter((element) => arr.indexOf(element) === -1)` * Using a map function with the `filter()` method: `arr.map((element) => element).filter((element, index, self) => self.indexOf(element) === index)` * Using an array's built-in `reduce()` method: `arr.reduce((acc, element) => acc.includes(element) ? acc : [element], [])`
Related benchmarks:
lodash uniq vs vanilla set - final
Lodash uniq vs Set to unique array
lodash uniq vs set - 3
lodash uniq vs deconstructed set
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?