Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniq vs Set transformation
(version: 0)
Comparing performance of:
Array from set vs uniq
Created:
5 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
const values = ['testing', 'test', 'foo', 'bar', 'test', 'test', 'test', 'testing', 'foo', 'bar', 'meow', 'hi', 'test', 'nothing', 'something']; Array.from(new Set(values))
uniq
const values = ['testing', 'test', 'foo', 'bar', 'test', 'test', 'test', 'testing', 'foo', 'bar', 'meow', 'hi', 'test', 'nothing', 'something']; _.uniq(values)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array from set
uniq
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):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** MeasureThat.net provides a JSON representation of a benchmark, which includes two test cases: `Array from set` and `uniq`. The first test case uses the built-in `Set` data structure in JavaScript to transform an array of values into a new array with unique elements. The second test case uses the popular utility library Lodash's `uniq` function to achieve the same result. **Options being compared** In this benchmark, two approaches are being compared: 1. **Built-in `Set`**: This approach uses the built-in `Set` data structure in JavaScript to transform an array of values into a new array with unique elements. The advantage of this approach is that it's a native implementation and likely to be fast. 2. **Lodash's `uniq` function**: This approach uses Lodash's `uniq` function, which is a utility function that removes duplicate elements from an array. The advantage of this approach is that it's a well-tested and optimized library function. **Pros and Cons** Here are some pros and cons for each approach: 1. **Built-in `Set`**: * Pros: Fast, native implementation. * Cons: May not work as expected if the input array contains non-unique elements (e.g., strings that have different representations in JavaScript), and may not be compatible with older browsers or environments. 2. **Lodash's `uniq` function**: * Pros: Well-tested, optimized library function; works with any type of data. * Cons: Requires an additional dependency (the Lodash library). **Library usage** In this benchmark, the Lodash library is used for the second test case (`uniq`). The library provides a convenient and efficient way to remove duplicate elements from an array. **Special JS feature or syntax** There are no special JavaScript features or syntax used in these test cases. However, it's worth noting that some modern browsers may optimize certain operations using WebAssembly or other techniques, which could affect the benchmark results. **Other alternatives** If you were to implement a custom `uniq` function without using an existing library like Lodash, here are some alternative approaches: 1. **Using a `Map`**: Create a map with the elements as keys and the values as arrays. Then, iterate over the original array and for each element, check if it exists in the map. If it does, remove it from the map. 2. **Using a regular expression**: Use a regular expression to match duplicate elements and replace them with unique ones. Here's an example of how you might implement a custom `uniq` function using a `Map`: ```javascript function uniq(arr) { const map = new Map(); return arr.filter((x) => { if (map.has(x)) { return false; } map.set(x, true); return true; }); } ``` Keep in mind that this implementation has the same pros and cons as the built-in `Set` approach.
Related benchmarks:
lodash uniq vs native uniq
Spread Set vs Lodash uniq
uniqBy vs stringify performance
uniqBy performance lodash vs native
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?