Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array from set vs lodash uniq
(version: 0)
Comparing performance of:
Array from set vs lodash uniq
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:
Array from set
var l = Array.from(new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])); return l;
lodash uniq
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
Array from set
lodash uniq
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array from set
2535594.8 Ops/sec
lodash uniq
4292113.0 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. **Benchmark Overview** The benchmark compares two approaches to remove duplicates from an array: using `Array.from` with a `Set` constructor, and using Lodash's `uniq` function. The goal is to determine which approach is faster for this specific use case. **Options Compared** 1. **Array.from(new Set([array]))**: This method uses the `Set` constructor to create an immutable set from the input array, which automatically removes duplicates. It then converts the set back into an array using `Array.from`. 2. **Lodash's `uniq(l)`**: Lodash's `uniq` function takes an array as input and returns a new array with duplicates removed. **Pros and Cons** 1. **Array.from(new Set([array]))**: * Pros: Simple, efficient, and well-supported by modern browsers. * Cons: May require additional setup (e.g., importing Lodash) for non-browser environments. 2. **Lodash's `uniq(l)`**: * Pros: Widely available, easy to use, and flexible (accepts various input types). * Cons: Adds dependency on Lodash, which may not be desirable in all situations. **Other Considerations** * The benchmark uses Chrome 123 as the test browser, which is a relatively modern version. * The tests are run on desktop platforms, which may affect performance compared to mobile or tablet devices. * The benchmark does not account for edge cases like empty input arrays or arrays with only unique elements. **Library and Syntax** Lodash's `uniq` function is used as part of the benchmark. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string formatting, and more. The `Array.from(new Set([array]))` approach uses modern JavaScript features like the `Set` constructor and `Array.from`. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes used in this benchmark. The code is straightforward and uses standard JavaScript constructs. Now, let's talk about alternative approaches: * **Using a custom implementation**: You could write your own function to remove duplicates from an array using a simple algorithm like iterating over the array and checking for matches. * **Other libraries**: Other libraries like Underscore.js or Ramda provide similar functionality to Lodash's `uniq` function. However, these alternatives may have different performance characteristics or use cases. * **Built-in methods**: Some browsers (like Firefox) have built-in methods for removing duplicates from an array, such as `Array.from(new Set(arr))`. These methods might offer better performance than the `uniq` function. In summary, the benchmark compares two approaches to remove duplicates from an array: using a modern JavaScript feature like `Set` and `Array.from`, and using Lodash's widely available `uniq` function. The choice of approach depends on your specific use case, desired dependencies, and performance requirements.
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?