<script>https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js</script>
var data = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}, {a: 6}, {a: 7}, {a: 8}, {a: 1}];
_.uniqBy(data, 'a');
const uniqueBy = (array, fieldName) => {
const uniqueArr = [];
const addedFields = {};
array.forEach((el) => {
if (addedFields[fieldName]) {
return;
}
addedFields[el[fieldName]] = true;
uniqueArr.push(el);
})
return uniqueArr;
}
uniqueBy(data, 'a')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
javascript |
Test name | Executions per second |
---|---|
lodash | 3576818.8 Ops/sec |
javascript | 5173925.0 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net, which compares two approaches to remove duplicate entries from an array: using Lodash's uniqBy
function and a custom implementation.
Library Used: Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks like data manipulation, string manipulation, and more. In this case, the uniqBy
function is used to remove duplicate entries from an array based on a specified field value.
Custom Implementation
The custom implementation uses a simple algorithm to achieve the same result as Lodash's uniqBy
. The main difference between the two approaches is that Lodash's uniqBy
sorts the array before removing duplicates, whereas the custom implementation creates an object with already added values to keep track of unique entries.
Options Compared
The benchmark compares two options:
uniqBy
function: This approach relies on sorting the array and uses a stable sort algorithm (e.g., merge sort) to preserve the original order of equal elements.Pros and Cons
Lodash's uniqBy
function:
Pros:
Cons:
Custom implementation:
Pros:
Cons:
Device and Browser Variations
The benchmark results show variations in execution times across different devices and browsers. These variations can be attributed to factors like:
Special JavaScript Features/ Syntax
This benchmark does not use any special JavaScript features or syntax that would require specific explanations. It focuses on the core implementation details of the uniqBy
function and custom implementation.
Alternative Approaches
Other approaches to remove duplicate entries from an array include:
Set()
and Array.from()
)moment.js
or uuid
Each of these alternatives has its own trade-offs in terms of performance, complexity, and maintainability.