<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var testArr = [];
for (var i = 0; i < 100000; i++) {
testArr.push({
val: String(getRandomInt(1000))
});
}
const { resultArray } = testArr.reduce((result, item) => {
if(!result.resultMap[item.val]){
result.resultMap[item.val] = true;
result.resultArray.push(item)
}
return result;
}, {resultArray: [], resultMap: {}});
return resultArray;
const resultArray = [];
const resultMap = {};
for(let item of testArr) {
if(!resultMap[item.val]){
resultMap[item.val] = true;
resultArray.push(item)
}
}
return resultArray;
const resultObj = testArr.reduce((result, item) => {
if(!result[item.val]){
result[item.val] = item;
}
return result;
}, {});
const resultArray = Object.values(resultObj);
return resultArray;
const resultObj = {};
for(let item of testArr) {
if(!resultObj[item.val]){
resultObj[item.val] = item;
}
}
const resultArray = Object.values(resultObj);
return resultArray;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array Unique | |
For Loop | |
Array Unique - v2 | |
For Loop - v2 |
Test name | Executions per second |
---|---|
Array Unique | 509.2 Ops/sec |
For Loop | 500.8 Ops/sec |
Array Unique - v2 | 421.2 Ops/sec |
For Loop - v2 | 400.8 Ops/sec |
I'll do my best to break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two approaches for finding unique elements in an array of objects: reduce()
with a custom mapping function versus a traditional for
loop.
reduce()
Approach
The first approach uses the reduce()
method, which is a functional programming technique that applies a reduction function to each element in an array. In this case, the reduction function maps each object to a boolean value indicating whether it's a unique element or not. The resulting array of unique elements is then returned.
for
Loop Approach
The second approach uses a traditional for
loop to iterate over the array and check for uniqueness. For each element, it checks if a corresponding key exists in an object (resultMap
) and updates the result array accordingly.
Pros and Cons
reduce()
or functional programmingfor
Loop Approach:Library: Lodash
The benchmark uses the Lodash library, which provides a reduce()
method and other utility functions. The reduce()
method is used in the first approach to map each object to a boolean value indicating uniqueness.
Special JS Feature/Syntax
None of the approaches explicitly use any special JavaScript features or syntax beyond the standard language.
Other Considerations
for
loop approach can be seen as less readable due to its verbosity and lack of abstraction.reduce()
approach reuses the same function logic for multiple use cases, while the for
loop approach requires duplicating code.Alternatives
Other approaches for finding unique elements in an array could include:
Map
) to store and check for uniqueness.Keep in mind that the choice of approach depends on the specific requirements, constraints, and performance characteristics of the use case.