Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique elements in array using filter - lodash 4.17.21
(version: 0)
Comparing performance of:
_.uniq vs set vs uniq by filter
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
Script Preparation code:
var elements = [1,2,3,1,2,4,2,3,5,3]
Tests:
_.uniq
_.uniq(elements)
set
[...new Set(elements)]
uniq by filter
elements.filter((v, i, a) => a.indexOf(v) === i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.uniq
set
uniq by filter
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/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.uniq
7902636.0 Ops/sec
set
4935947.0 Ops/sec
uniq by filter
11516957.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the explanation into smaller parts to make it easier to understand. **Benchmark Definition JSON** The provided JSON represents a benchmark test case for measuring the performance of JavaScript code that removes duplicate elements from an array. The test has three variants: 1. Using Lodash's `uniq` function (version 4.17.21). 2. Creating a new Set and spreading its elements into an array. 3. Filtering the original array using a callback function. The script preparation code defines an array `elements` with duplicate values, and the HTML preparation code includes the necessary Lodash library file (`lodash.js`) version 4.17.21. **Options Compared** Each option has its pros and cons: 1. **Lodash's `uniq` function**: This is a popular utility function that removes duplicates from an array while preserving order. The pros include: * Efficient implementation (O(n) complexity). * Simple usage. * Well-documented. The cons include: * External dependency on Lodash library. * May not be suitable for all use cases (e.g., when preserving the original array's index). 2. **Creating a new Set and spreading its elements**: This approach is simple, efficient, and doesn't require any external libraries. * Pros: + Lightweight (no external dependency). + Fast (O(n) complexity). + Easy to understand and implement. The cons include: + May not preserve the original array's index. + Requires manual handling of null or undefined values. 3. **Filtering the original array**: This approach uses a callback function to filter out duplicates while preserving the original array's order. * Pros: + Preserves the original array's index. + Simple implementation. The cons include: + May have performance issues (O(n^2) complexity in the worst case). + More complex implementation compared to the Set-based approach. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string manipulation, and more. The `uniq` function is one of its most useful utilities for removing duplicates from arrays while preserving order. **Special JS Feature/ Syntax (None)** There are no special JavaScript features or syntaxes used in this benchmark test case. **Other Alternatives** If you need to measure the performance of other approaches, here are some alternatives: * Using `Array.prototype.filter()` and `Array.prototype.indexOf()` * Implementing a custom implementation using loops or recursive functions * Using other libraries like ` underscore` or `ramda` Keep in mind that each approach has its own strengths and weaknesses, and the choice of which one to use depends on your specific requirements and preferences.
Related benchmarks:
unique elements in array using filter
Lodash - uniq2
returns unique values in array
unique elements in array using filter fork
Comments
Confirm delete:
Do you really want to delete benchmark?