Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array unique insert: lodash union vs object.assign vs uniq
(version: 0)
Comparing performance of:
lodash union vs lodash uniq vs lodash includes and push
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
lodash union
var a = [ 1, 2, -1, 3, 4, 5, 10000000001, 6, 7, 8, 9, 1000, 1001, 100001, 100000001 ]; var c = _.union(a, [5000]);
lodash uniq
var a = [ 1, 2, -1, 3, 4, 5, 10000000001, 6, 7, 8, 9, 1000, 1001, 100001, 100000001 ]; var c = _.uniq([...a, 5000]);
lodash includes and push
var a = [ 1, 2, -1, 3, 4, 5, 10000000001, 6, 7, 8, 9, 1000, 1001, 100001, 100000001 ]; var c = _.includes(a, 5000) ? [...a] : [...a, 5000];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash union
lodash uniq
lodash includes and push
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 the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The benchmark measures the performance of three different approaches for inserting a new element into an array: Lodash's `_.union`, `_.uniq`, and `_.includes` with push. The test case uses a large array `a` containing 14 elements, one of which is repeated multiple times (10000000001), and then inserts the value 5000. **Options Compared** 1. **Lodash's `_.union`**: This method returns a new array with all unique elements from both arrays. 2. **Lodash's `_.uniq`**: This method removes duplicate elements from an array, returning a new array without duplicates. 3. **Lodash's `_.includes` with push**: This approach checks if the element is already in the array using `_.includes`, and if not, pushes it to the end of the array. **Pros and Cons of Each Approach** 1. **Lodash's `_.union`**: * Pros: Easy to read and understand, creates a new array with unique elements. * Cons: Creates an additional array object, can be slower for very large datasets due to copying. 2. **Lodash's `_.uniq`**: * Pros: Faster than `_.union`, as it only needs to iterate through the original array once. * Cons: Can be less intuitive, especially if not familiar with the implementation details. 3. **Lodash's `_.includes` with push**: * Pros: Simple and efficient, as it only checks for membership and pushes if necessary. * Cons: May require additional logic to handle edge cases (e.g., arrays with non-array elements). **Library and Purpose** The Lodash library is a popular JavaScript utility library that provides a collection of functional programming helpers. In this benchmark, Lodash's `_.union`, `_.uniq`, and `_.includes` are used for their respective purposes. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. It only relies on standard JavaScript arrays and Lodash functions. **Other Alternatives** In addition to the approaches tested, other methods could be used to insert a new element into an array: * `concat()`: Creates a new array by concatenating two arrays. * `splice()`: Modifies the original array by adding a new element at a specified index. * `Array.prototype.push()`: Appends one or more elements to the end of an array. These alternatives may have different performance characteristics, memory usage, and readability compared to the approaches tested in this benchmark.
Related benchmarks:
lodash uniq vs native uniq
uniqBy vs stringify performance
lodash uniqBy vs custom uniqBy
Lodash uniqBy vs Javascript uniqBy
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?