Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs vanilla set - final
(version: 0)
Comparing performance of:
Set vs Uniq
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js'></script>
Tests:
Set
return [...new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])];
Uniq
return _.uniq([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]);
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
3102821.0 Ops/sec
Uniq
8667120.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Overview** The benchmark is comparing two approaches to removing duplicates from an array of numbers: using the built-in `Set` object (also known as "vanilla set") in JavaScript, and using the `uniq()` function from the Lodash library. **Options Compared** There are two options being compared: 1. **Vanilla Set**: This approach uses the `Set` object to remove duplicates from an array. A `Set` is a collection of unique values, which means that if you try to add a value that's already in the set, it won't be added again. 2. **Lodash Uniq**: This approach uses the `uniq()` function from Lodash, which removes duplicate elements from an array. **Pros and Cons** Here are some pros and cons of each approach: * **Vanilla Set**: + Pros: Built-in to JavaScript, fast, and lightweight. + Cons: May not work well with arrays that contain non-numeric values, as `Set` only works with primitive values (like numbers, strings, etc.). * **Lodash Uniq**: + Pros: More flexible than vanilla set, can handle arrays with non-numeric values, and provides additional functionality like sorting and stability options. + Cons: Requires an external library (Lodash), which adds a small overhead. **Library Used** The Lodash library is being used in this benchmark. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like data manipulation, string processing, and more. The `uniq()` function from Lodash is being used to remove duplicates from the array. **Special JS Feature or Syntax** There isn't any special JavaScript feature or syntax being tested in this benchmark. However, it's worth noting that some modern JavaScript versions (like ES6+) have introduced new features like template literals and async/await, which are not being used in this benchmark. **Other Alternatives** If you wanted to implement a duplicate removal algorithm without using the `Set` object or Lodash, here are some alternative approaches: 1. **Using an array with filtering**: You can use the `filter()` method to remove duplicates from an array. This approach has a time complexity of O(n^2) in the worst case, making it slower than the `Set` object. ```javascript const arr = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7]; const uniqueArr = arr.filter((val, index) => arr.indexOf(val) === index); ``` 2. **Using a Map**: You can use a `Map` object to remove duplicates from an array. A `Map` is similar to a `Set`, but it preserves the original order of elements. ```javascript const map = new Map(); arr.forEach((val) => { map.set(val, true); }); const uniqueArr = Array.from(map.keys()); ``` These alternative approaches have different trade-offs in terms of performance and complexity.
Related benchmarks:
lodash uniq vs native uniq
uniqBy performance
uniqBy vs stringify performance
uniqBy performance lodash vs native
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?