Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set 20230703
(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>
Script Preparation code:
const items = []; const LIMIT = 200; for (let i = 0; i < 1000; ++i) { items[i] = i % LIMIT; } window.ITEMS = items;
Tests:
Set
var l = new Set(window.ITEMS); return l;
Array
var l = window.ITEMS; 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):
Let's break down what is tested in the provided JSON benchmark. **Benchmark Definition** The benchmark defines two test cases: `Set` and `Array`. These test cases are comparing the performance of using a `Set` data structure versus an array to remove duplicates from a large dataset. **Script Preparation Code** The script preparation code generates a large array of numbers, where each number is a unique value between 0 and 199 (since the array length is limited by the `LIMIT` constant). This array is stored in the global variable `window.ITEMS`. **Html Preparation Code** The HTML preparation code includes a reference to the Lodash library, specifically the `_uniq` function, which is used in one of the test cases. **Individual Test Cases** 1. **Set**: In this test case, a new `Set` object is created from the global variable `window.ITEMS`. A `Set` is an unordered collection of unique values. This approach has some pros: * It ensures that only unique values are stored in the collection. * It provides fast lookup and insertion times (O(1) average case). * However, it also has some cons: * Creating a new object can be expensive in terms of memory allocation and garbage collection. * Some browsers might not support `Set` objects or have issues with their implementation. 2. **Array**: In this test case, the global variable `window.ITEMS` is directly used as an array. This approach has some pros: * It's a lightweight and straightforward way to remove duplicates from an array. * Most browsers support arrays, making it a widely compatible option. * However, it also has some cons: * If not done correctly, this approach can lead to slower performance due to the need for additional iterations or sorting. **Library: Lodash** In both test cases, the `Set` object is created using Lodash's `_uniq` function. The `_uniq` function takes an array as input and returns a new array with duplicates removed. This function uses a combination of data structures (e.g., `Set`) to achieve efficient duplicate removal. **Special JS feature/Syntax** There are no special JavaScript features or syntaxes being used in these test cases, other than the use of the `_uniq` function from Lodash. **Alternatives** Other alternatives for removing duplicates from an array include: * Using a regular expression with `String.prototype.split()` and `Array.prototype.concat()` * Implementing a custom duplicate removal algorithm using sorting or hashing * Using a different library, such as `Array.prototype.filter()` or `Underscore.js` Keep in mind that the performance of these alternatives might vary depending on the specific use case and browser environment.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
lodash uniq vs spread new Set() medium size
Uniq by sorting test
Uniq by sorting test 2
Comments
Confirm delete:
Do you really want to delete benchmark?