function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var arr = [];
for(var i = 0; i < 100000; i++){
arr.push({valueF:getRandomInt(100), valueM:getRandomInt(100)});
}
result = _(arr)
.filter((value) =>
value.valueF > 50
)
.map((value) => ({
"value": value.valueM
}))
.value();
console.log(result);
const result = arr
.filter(value => value.valueF > 50)
.map(value => ({ value: value.valueM }));
console.log(result);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Vanilla Javascript |
Test name | Executions per second |
---|---|
Lodash | 106.3 Ops/sec |
Vanilla Javascript | 132.2 Ops/sec |
In this benchmark, test cases measure the performance difference between using the Lodash library for functional programming style and Vanilla JavaScript for similar data manipulations on an array of objects.
result = _(arr)
.filter((value) => value.valueF > 50)
.map((value) => ({ "value": value.valueM }))
.value();
console.log(result);
const result = arr
.filter(value => value.valueF > 50)
.map(value => ({ value: value.valueM }));
console.log(result);
filter
and map
).Based on the benchmark results:
This indicates that in this specific case, Lodash was faster, likely due to optimizations in the library for handling larger datasets, despite the common perception that native methods are generally preferable for performance.
forEach
, reduce
, and other Array methods can also achieve similar goals efficiently.In conclusion, engineers need to weigh the trade-offs between using a utility library like Lodash versus raw JavaScript considering readability, maintainability, performance, and context of use when developing their applications.