<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
var max1 = 100000; // 100,000 (100 Thousand)
var max2 = 10000000; // 10,000,000 (10 Million)
var max3 = 100000000; // 100,000,000 (100 Million)
var arr1 = [];
//for (var i = 0; i <= max1; i++) { arr1.push(i); }
var arr2 = [];
for (var i = 0; i <= max2; i++) { arr2.push({name: i, value: Math.floor(Math.random() * 100) }); }
var arr3 = [];
//for (var i = 0; i <= max3; i++) { arr3.push(i); }
const removeDuplicates = (objects, propertyName) => {
const uniqueValues = new Set();
return objects.filter((o) => {
const isNewValue = !uniqueValues.has(o[propertyName]);
uniqueValues.add(o[propertyName]);
return isNewValue;
});
};
removeDuplicates(arr2, 'value')
const removeDuplicates = (objects, propertyName) => {
_.uniqBy(objects, propertyName)
};
removeDuplicates(arr2, 'value')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native | |
Lodash.js filter |
Test name | Executions per second |
---|---|
Native | 2.9 Ops/sec |
Lodash.js filter | 3.6 Ops/sec |
Benchmark Overview
The provided JSON represents a benchmark test case created on the MeasureThat.net website, which compares the performance of two approaches to remove duplicates from an array: a native JavaScript implementation and a Lodash.js function.
Native JavaScript Implementation
The native JavaScript implementation uses the filter()
method to remove duplicates. It creates a Set
object (uniqueValues
) to keep track of unique values. The filter()
method iterates through the array, and for each element, it checks if the value is new by checking if it's not in the Set
. If it's new, the value is added to the Set
, and the element is included in the resulting array.
Pros:
Cons:
Set
object.Lodash.js Implementation
The Lodash.js implementation uses the _uniqBy()
function from the Lodash library. This function takes two arguments: an array (objects
) and a property name (propertyName
). It returns a new array with duplicates removed based on the specified property.
Pros:
Cons:
Library: Lodash.js
Lodash is a popular JavaScript utility library that provides a collection of functional programming helpers. The _uniqBy()
function is one of these helpers, designed to remove duplicates from an array based on a specified property.
The _.uniqBy()
function uses a combination of the Set
data structure and the filter()
method to achieve its performance benefits. This approach ensures that the function can scale well for large datasets while maintaining efficient execution times.
Special JS Feature/Syntax
There is no special JavaScript feature or syntax used in this benchmark test case. The implementation relies on standard JavaScript features, such as the Set
object and array methods like filter()
and push()
.
Alternatives
If you want to implement a similar benchmark or test the performance of other approaches to remove duplicates from an array, here are some alternatives:
Set
, you could use another data structure like a Map
or a binary search tree (BST) to keep track of unique values.Keep in mind that each alternative has its pros and cons, and the choice ultimately depends on your specific requirements, performance considerations, and personal preferences.