Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set with strings
(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 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:
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/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
4668410.0 Ops/sec
Array
5561220.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. The test aims to compare the performance of two approaches: using a `Set` data structure and using the `uniq()` function from the Lodash library, both with arrays containing duplicate values. **Approach 1: Using a Set** This approach uses a native JavaScript `Set` object to store unique elements. A `Set` is an unordered collection of unique values. When you add a value to a `Set`, it automatically removes any duplicates. In this case, the array `[ '1', '2', '3', '4', '5', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7' ]` is converted to a `Set`, which removes the duplicate values, resulting in a new array `[ '1', '2', '3', '4', '5', '6', '7' ]`. The pros of this approach are: * Native JavaScript support, which means it's widely supported and doesn't require any external dependencies. * Good cache performance, as the `Set` object uses a hash table internally. However, there are some cons to consider: * The size of the resulting array might be larger than expected due to the way JavaScript handles duplicates in arrays. * Some modern browsers (like Chrome) have optimized their internal data structures to improve performance for `Set` operations. **Approach 2: Using Lodash's uniq() function** In this approach, a library (Lodash) is used to remove duplicate values from an array. The `uniq()` function takes an array as input and returns a new array with unique elements. The pros of this approach are: * Convenience: it's easy to use and requires minimal code changes. * Well-tested and widely supported. However, there are some cons to consider: * External dependency: the benchmark uses Lodash, which means that any browser without Lodash support might not be able to run the benchmark correctly. * Potential performance impact due to the need to parse and execute JavaScript code for the Lodash library. * Might not optimize as well as the native `Set` approach. **Library used:** Lodash Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like data manipulation, array and object operations, and more. The `uniq()` function in Lodash is part of this utility set and is commonly used to remove duplicate values from arrays. **Special JS feature or syntax: None** There are no special JavaScript features or syntaxes being tested in these benchmarks. **Other alternatives:** 1. Using an alternative library (e.g., `underscore`) instead of Lodash. 2. Implementing the unique value removal logic manually, without using a built-in function like `uniq()`. 3. Comparing performance with other data structures like `Map` or `WeakSet`. These alternatives would require different approaches to benchmarking and might yield different results depending on the specific implementation and use case. Keep in mind that measuring the performance of small benchmarks can be influenced by various factors, such as system load, memory constraints, and caching effects. These examples are meant to illustrate a basic understanding of how microbenchmarks work and should not be taken as definitive or absolute measures of performance.
Related benchmarks:
lodash uniq vs vanilla set - final
lodash uniq vs native uniq
uniqBy vs stringify performance
Lodash uniqBy vs Javascript uniqBy
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?