Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique Lodash vs Set
(version: 0)
Comparing performance of:
Lodash vs Set Spread based vs Set ArrayFrom based vs Object Based Unique
Created:
5 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.5/lodash.min.js'></script>
Script Preparation code:
var a = [],i; for (i = 0; i < 1000; i++) a.push(Math.floor(Math.random() * 1000) + 1); var unique = (function(){ let keys = Object.keys; return function(arr){ let ob = {}; let len = arr; let retArr=[]; for(let i=0;i<len;i++){ if(ob[arr[i]] === undefined){ ob[arr[i]]=1; retArr.push(arr[i]); } } return retArr; } }());
Tests:
Lodash
_.uniq(a)
Set Spread based
[... new Set(a)]
Set ArrayFrom based
Array.from(new Set(a))
Object Based Unique
unique(a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash
Set Spread based
Set ArrayFrom based
Object Based Unique
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):
I'll explain the benchmark and its options. **Benchmark Overview** The benchmark measures the performance of three different approaches to remove duplicates from an array: Lodash's `uniq` function, `Set` spread operator (`[... new Set(a)]`), and `Array.from(new Set(a))`. The benchmark uses a fixed input size of 1000 random integers. **Options Compared** 1. **Lodash's `uniq` function**: This approach uses the `Object.keys` function to get an array of keys from the input array, which is then used to create a new object with unique values as keys. The resulting values are returned as an array. 2. **Set spread operator (`[... new Set(a)]`)**: This approach uses the spread operator to create a new `Set` object from the input array, which automatically removes duplicates. The `Set` object is then converted back to an array using the spread operator again. 3. **Array.from(new Set(a))**: This approach creates a new `Set` object from the input array and then uses the `Array.from()` method to convert it back to an array. **Pros and Cons of Each Approach** 1. **Lodash's `uniq` function**: * Pros: Can be used with other Lodash functions, has built-in support for handling multiple values. * Cons: May have performance overhead due to the use of `Object.keys`. 2. **Set spread operator (`[... new Set(a)]`)**: * Pros: Fast and efficient, easy to read and write. * Cons: Requires a modern browser that supports the spread operator. 3. **Array.from(new Set(a))**: * Pros: Also fast and efficient, can be used with other array methods. * Cons: May have performance overhead due to the creation of an intermediate `Set` object. **Library Used** The benchmark uses Lodash version 4.17.5, which is a popular utility library for JavaScript that provides various functions for working with arrays, objects, and more. **Special JS Features/Syntax** None are mentioned in this specific benchmark. **Other Considerations** When choosing an approach, consider the following factors: * Performance: The `Set` spread operator and `Array.from(new Set(a))` approaches are likely to be the fastest. * Browser support: If you need to support older browsers, Lodash's `uniq` function may be a better choice. * Code readability: The `Set` spread operator approach is often considered more readable than Lodash's `uniq` function. **Alternatives** Other alternatives for removing duplicates from an array include: 1. Using a `Map` object with the input array as values, which automatically removes duplicates. 2. Using a custom implementation that uses a hash table or other data structure to keep track of unique elements. 3. Using a library like `fast-json-stamp` which provides a fast and efficient way to remove duplicates from an array. Note: The performance of these alternatives may vary depending on the specific use case and requirements.
Related benchmarks:
Unique lodash vs vanilla
Lodash difference vs Set & Filter Larger and Random
Lodash.js vs Native values
Lodash.js vs Native map
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
Comments
Confirm delete:
Do you really want to delete benchmark?