Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set vs indexof
(version: 0)
Comparing performance of:
Set vs Lodash unique vs Index Of
Created:
6 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;
Lodash unique
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
Index Of
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return l.filter(function(elem, pos) { return l.indexOf(elem) == pos; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Lodash unique
Index Of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
9488148.0 Ops/sec
Lodash unique
24356136.0 Ops/sec
Index Of
10822368.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares three approaches to remove duplicates from an array: 1. **Set**: Using a built-in JavaScript `Set` object, which automatically removes duplicates. 2. **Lodash unique**: Utilizing the `uniq` function from the popular utility library Lodash. 3. **Index Of**: Implementing a custom solution using `filter` and `indexOf` methods. **What is being tested?** In each test case: * A large array with multiple instances of the same element (e.g., `[1, 2, 3, 4, 5, 6, 7, 7, 7, 7, ...]`) is created. * The benchmark measures how long it takes to run each test case. **Options Compared** The three options are compared in terms of their execution time: | Option | Description | | --- | --- | | Set | Using a built-in JavaScript `Set` object to remove duplicates. | | Lodash unique | Utilizing the `uniq` function from Lodash, a popular utility library. | | Index Of | Implementing a custom solution using `filter` and `indexOf` methods. | **Pros and Cons of Each Approach** 1. **Set**: * Pros: Simple, efficient, and reliable. * Cons: Requires a JavaScript `Set` object, which might not be available in older browsers or Node.js versions. 2. **Lodash unique**: * Pros: Well-maintained, widely used, and provides additional utility functions. * Cons: Adds external library dependencies, which may increase bundle size and overhead. 3. **Index Of**: * Pros: Custom solution can be optimized for specific use cases or performance characteristics. * Cons: More complex implementation, potentially slower due to the use of `filter` and `indexOf`. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as: * String manipulation * Array operations (e.g., `uniq`, `map`, `reduce`) * Object manipulation * Math and number utilities In this benchmark, the `uniq` function from Lodash is used to remove duplicates from an array. **Special JS Feature or Syntax** The benchmark does not use any special JavaScript features or syntax that would require additional explanation. The code only relies on standard JavaScript language features. **Alternatives** If you need to remove duplicates from an array, other alternatives could be: * Using `Array.prototype.filter()` and checking for the existence of each element in the original array using `in` operator. * Utilizing a third-party library like Ramda or Underscore.js (similar to Lodash). * Implementing a custom solution using a data structure like a hash table or a trie. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
lodash uniq vs native uniq
uniqBy performance
get uniq values js
uniqBy performance lodash vs native
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?