Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create an array with unique values - Lodash Uniq vs Javascript Set
(version: 1)
Comparing performance of:
Lodash Uniq vs Javascript Set
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Tests:
Lodash Uniq
let counter = 1e5; const arr = []; while(counter--) { arr.push(i); arr.push(i); } _.uniq(arr);
Javascript Set
let counter = 1e5; const set = new Set(); while(counter--) { set.add(i); set.add(i); } [...set];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash Uniq
Javascript Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash Uniq
377.0 Ops/sec
Javascript Set
1228.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared options, their pros and cons, and other considerations. **Benchmark Overview** The benchmark compares two approaches to remove duplicates from an array: 1. **Lodash Uniq**: Uses the `uniq` function from the Lodash library, which creates a new array with unique values. 2. **JavaScript Set**: Uses a JavaScript `Set` object to store unique values and then converts it back to an array. **Options Compared** The two options are compared in terms of: * **Efficiency**: How quickly each approach can process large datasets. * **Memory Usage**: The amount of memory required by each approach. * **Readability**: How easy or hard the code is to understand and maintain. **Pros and Cons** 1. **Lodash Uniq** * Pros: + Higher-level abstraction, making the code more concise and readable. + Includes additional features like sorting and stability (preserving order of equal elements). * Cons: + External dependency on Lodash library, which may not be included in all projects. + May have slower performance due to function call overhead. 2. **JavaScript Set** * Pros: + No external dependencies or libraries. + More efficient and faster performance since it uses a built-in data structure. * Cons: + Requires manual array conversion, which can be error-prone and less readable. **Other Considerations** * **Memory Safety**: Both approaches have the same memory safety concerns since they both modify the original array. * **Scalability**: JavaScript Set is generally more suitable for large datasets due to its efficient use of memory and processing power. * **Code Quality**: Lodash Uniq may be considered better code quality in terms of conciseness, readability, and maintainability. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, string manipulation, and functional programming. The `uniq` function is one of its most useful utilities, making it easy to remove duplicates from arrays. The provided benchmark code snippet includes the necessary import statement for Lodash, allowing users to easily compare the performance of this approach with JavaScript Set. **Special JS Feature/ Syntax** This benchmark does not require any special JavaScript features or syntax, making it accessible to a wide range of developers. **Alternatives** Other alternatives to remove duplicates from an array include: 1. Using `Array.prototype.filter()` and `Array.prototype.includes()`. 2. Implementing a custom solution using a data structure like a hash table. 3. Using a library like Moment.js for string manipulation (although not applicable here). These alternatives may have varying trade-offs in terms of performance, readability, and maintainability, but can be worth exploring depending on specific use cases or project requirements.
Related benchmarks:
uniqBy vs stringify performance
Lodash uniq vs Set to unique array
lodash uniqBy vs custom uniqBy
Lodash - uniq
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?