<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.min.js"></script>
</head>
<body>
</body>
</html>
var products = [
{
name: "Foo",
id: 1,
color: "red",
price: {
beforeSale: 14.11,
afterSale: 11.11,
currency: "USD"
}
},
{
name: "Foo2",
id: 2,
color: "green",
price: {
beforeSale: 24.22,
afterSale: 22.22,
currency: "USD"
}
},
{
name: "Foo3",
id: 3,
color: "blue",
price: {
beforeSale: 44.33,
afterSale: 33.33,
currency: "USD"
}
},
{
name: "Foo4",
id: 4,
color: "red",
price: {
beforeSale: 54.44,
afterSale: 44.44,
currency: "USD"
}
},
{
name: "Bar",
id: 5,
color: "green",
price: {
beforeSale: 64.55,
afterSale: 55.55,
currency: "USD"
}
},
{
name: "Bar2",
id: 6,
color: "blue",
price: {
beforeSale: 74.66,
afterSale: 66.66,
currency: "USD"
}
},
{
name: "Bar3",
id: 7,
color: "red",
price: {
beforeSale: 84.77,
afterSale: 77.77,
currency: "USD"
}
},
{
name: "Bar4",
id: 8,
color: "green",
price: {
beforeSale: 94.88,
afterSale: 88.88,
currency: "USD"
}
},
{
name: "Baz",
id: 9,
color: "blue",
price: {
beforeSale: 104.99,
afterSale: 99.99,
currency: "USD"
}
}
];
// Lodash's filter()
function testLodashFilter(color) {
return _.filter(products, function (product) {
return product.color === color;
});
}
// Native Array.prototype.filter()
function testFilter(color) {
return products.filter(function (product) {
return product.color === color;
});
}
// For loop
function testFor(color) {
var i;
var results = [];
for (i = 0; i < products.length; i++) {
if (products[i].color === color) {
results.push(products[i]);
}
}
return results;
}
// While loop
function testWhile(color) {
var i = 0;
var results = [];
while(i < products.length) {
if (products[i].color === color) {
results.push(products[i]);
}
i++;
}
return results;
}
testLodashFilter("red");
testFilter("red");
testFor("red");
testWhile("red");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash Filter | |
Native filter | |
For Loop | |
Whlie Loop |
Test name | Executions per second |
---|---|
Lodash Filter | 1605911.6 Ops/sec |
Native filter | 4183944.5 Ops/sec |
For Loop | 524993.2 Ops/sec |
Whlie Loop | 538727.2 Ops/sec |
Let's dive into the provided benchmark and explore what's being tested, the different approaches compared, their pros and cons, and other considerations.
Benchmark Definition
The benchmark is designed to test which approach is the most efficient for filtering an array of objects based on an attribute (in this case, color). The benchmark creates a sample dataset products
containing 10 objects with varying colors. It then defines four test cases:
filter()
function to filter the products by color.filter()
method on the Array.prototype
for filtering products by color.Options Compared The benchmark compares the performance of four different approaches:
filter()
functionArray.prototype.filter()
Pros and Cons
Other Considerations
Interpretation of Latest Benchmark Result
The latest benchmark result shows the relative performance of each approach on a single dataset. Lodash's filter()
function appears to be the fastest, followed closely by the Native Array.prototype.filter()
. The For Loop and While Loop are slower, likely due to the overhead from manual iteration.
Keep in mind that this benchmark is specific to the provided test case and may not generalize well to other scenarios or datasets. To gain a more comprehensive understanding of each approach's performance characteristics, additional benchmarks with varying dataset sizes and environments would be necessary.