function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var arr = [];
for(var i = 0; i < 100000; i++){
arr.push({value:getRandomInt(100)});
}
result = _(arr)
.filter((value) =>
value > 50
)
.map((value) => ({
"value": value
}))
.value();
console.log(result);
const result = arr
.filter(value => value > 50)
.map(value => ({ value }));
console.log(result);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Vanilla Javascript |
Test name | Executions per second |
---|---|
Lodash | 580.9 Ops/sec |
Vanilla Javascript | 556.8 Ops/sec |
This benchmark compares the performance of two approaches to filtering and mapping an array of objects in JavaScript: one using the Lodash library and the other using vanilla JavaScript.
getRandomInt(max)
generates random integers between 0 and 99.arr
is populated with 100,000 objects, where each object contains a single property value
with random integer values.Lodash Version:
result = _(arr)
.filter((value) => value > 50)
.map((value) => ({ "value": value }))
.value();
console.log(result);
_()
function initializes a Lodash chain, allowing for a sequence of method calls (filter
and map
) to be executed in a readable, chainable manner.Vanilla JavaScript Version:
const result = arr
.filter(value => value > 50)
.map(value => ({ value }));
console.log(result);
filter
and map
.Pros:
Cons:
Pros:
Cons:
In this benchmarking test, both Lodash and vanilla JavaScript demonstrate their capabilities for filtering and mapping operations. While Lodash provides a more expressive and potentially optimized approach in some scenarios, vanilla JavaScript showcases its performance advantage without the added complexity of library dependencies. Each approach has its merits, and the decision to use one over the other will depend on the specific needs of the project and the preferences of the development team.