Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs array.from set
(version: 0)
Comparing performance of:
Set vs Array
Created:
2 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 Array.from(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:
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 break down the provided benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The benchmark compares two approaches to remove duplicates from an array: 1. Using `Array.from()` and a regular Set (Set Approach) 2. Using Lodash's `uniq` function (Lodash Approach) **Set Approach (Individual Test Case)** ```javascript var l = new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]);\r\nreturn Array.from(l); ``` In this test case: * A Set is created with duplicate values (e.g., multiple occurrences of the number `7`). * The `Array.from()` function is used to convert the Set to an array. * The resulting array is returned. **Lodash Approach (Individual Test Case)** ```javascript var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7];\r\nreturn _.uniq(l); ``` In this test case: * An array is created with duplicate values (e.g., multiple occurrences of the number `7`). * The Lodash `uniq` function is used to remove duplicates from the array. * The resulting array without duplicates is returned. **Comparison and Pros/Cons** Both approaches achieve similar results: removing duplicates from an array. However, there are differences in their behavior: 1. **Performance:** Set Approach is likely to be faster since it leverages built-in JavaScript functionality (`Array.from()` and `Set`). Lodash Approach uses a custom implementation, which might incur additional overhead. 2. **Memory Usage:** Set Approach requires more memory to store the duplicate values, while Lodash Approach only stores unique values in the resulting array. 3. **Functionality:** Both approaches return an array without duplicates, but Lodash Approach is part of a larger utility library and may offer additional features. **Other Considerations** 1. **Browser Support:** Set Approach is supported by most modern browsers, while Lodash Approaches may require a polyfill or specific browser versions. 2. **Library Dependence:** Using Lodash requires including the library in your project, which might add unnecessary overhead for smaller projects or those with limited dependencies. **Alternatives** For removing duplicates from an array: 1. Using `Array.from()` and a regular Set: ```javascript var arr = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; var uniqueArr = Array.from(new Set(arr)); ``` 2. Using the `filter()` method: ```javascript var arr = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; var uniqueArr = arr.filter((val, idx) => { return arr.indexOf(val) === idx; }); ``` Keep in mind that these alternatives might not be as efficient or feature-rich as the Set Approach or Lodash Approach. When to use each approach: * Use Set Approach when performance and memory efficiency are crucial. * Use Lodash Approach when you need a lightweight, easy-to-use solution with additional utility functions from the Lodash library.
Related benchmarks:
lodash uniq vs native uniq
uniqBy vs stringify performance
lodash uniq vs set - 3
Lodash - uniq
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?