<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 sortBy = (key) => {
return (a, b) => (a[key] > b[key]) ? 1 : (b[key] > a[key] ? -1 : 0);
};
const uniqBy = (arr, a) => {
arr.sort(sortBy(a));
const arr1 = [];
arr1.push(arr[0]);
for (let index = 1; index < arr.length; index++) {
if (arr[index-1] && arr[index -1].a !== arr[index].a) {
arr1.push(arr[index]);
}
}
return arr1;
}
uniqBy(data, 'a')
function uniqueBy(arr, prop){
const record = []
const seen = {}
for (let i = 0, len = arr.length; i < len; ++i) { // Notice the len = arr.length
const item = arr[i]
const val = item[prop]
if (!seen[val]) {
seen[val] = 1
record.push(item)
}
}
}
uniqueBy(data, 'a')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
javascript | |
native |
Test name | Executions per second |
---|---|
lodash | 10302855.0 Ops/sec |
javascript | 6813315.5 Ops/sec |
native | 8606958.0 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Overview
The benchmark is designed to compare the performance of three approaches to remove duplicates from an array:
uniqBy
function for removing duplicates based on a specified property.uniqBy
function using only standard JavaScript features.Options being compared
The main options being compared are:
lodash.core.js
library to provide the uniqBy
function.uniqBy
function using standard JavaScript features, such as sorting and iterating over the array.Pros and Cons of each approach
Library explanation
In this benchmark, lodash
is used as a library to provide the uniqBy
function. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and more. In this case, the uniqBy
function is specifically designed to remove duplicates from an array based on a specified property.
Native JavaScript feature explanation
The custom implementation of uniqBy
in Native JavaScript uses standard JavaScript features, such as:
sort()
method is used to sort the array by the specified property.Other considerations
len = arr.length
in the Native JavaScript implementation is interesting. This is because array lengths are not always accurately represented by the length
property in older browsers. Using arr.length
ensures that the loop iterates over the correct number of elements, even in cases where length
might be out of date.Alternatives
The benchmark only shows three alternatives: Lodash, Native JavaScript, and an unknown alternative (not implemented). There may be other approaches or libraries that could be used to implement uniqBy
, such as:
uniq
function.: Using the
filter()` method to remove duplicates from an array can also be effective, although it might not be as efficient as sorting and iterating over the array.Overall, this benchmark highlights the importance of considering performance and optimization when implementing algorithms or using external libraries in JavaScript applications.