<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script>
underscore = _;
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.min.js"></script>
<script>
lodash = _;
</script>
</head>
<body>
</body>
</html>
var PromosArray = [
{
type: "regular",
display: "$123.00"
},
{
type: "markdown",
display: "$100.00"
},
{
type: "promo",
display: "$80.00"
}
];
function testArrayIteration() {
var items = [];
var i;
for (i = 0; i < PromosArray.length; i++) {
items.push(PromosArray[i].type);
}
return items;
}
function testArrayMap() {
return PromosArray.map(function (promo) {
return promo.type;
});
}
function testUnderscoreMap() {
return underscore.map(PromosArray, function (promo) {
return promo.type;
});
}
function testLodashMap() {
return lodash.map(PromosArray, function (promo) {
return promo.type;
});
}
function testForLoop() {
var newArray = new Array(PromosArray.length);
for (var i = 0; i < PromosArray.length; i++) {
newArray[i] = PromosArray[i].type;
}
return newArray;
}
testArrayIteration();
testArrayMap();
testUnderscoreMap();
testLodashMap();
testForLoop();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
testArrayIteration | |
testArrayMap | |
testUnderscoreMap | |
testLodashMap | |
testForLoop |
Test name | Executions per second |
---|---|
testArrayIteration | 1270250.0 Ops/sec |
testArrayMap | 1542518.2 Ops/sec |
testUnderscoreMap | 1847689.5 Ops/sec |
testLodashMap | 1861179.5 Ops/sec |
testForLoop | 988817.4 Ops/sec |
I'd be happy to help explain the benchmark.
Benchmark Overview
The benchmark is designed to test the performance of different methods for iterating over an array in JavaScript. The array contains three elements, each with a "type" property. The benchmark measures the time it takes to extract the "type" property from each element using four different approaches: map()
, underscore.map()
, lodash.map()
, and a simple for
loop.
Options Compared
map()
method from the Underscore.js library, which provides utility functions for functional programming.map()
method from the Lodash library, which provides a comprehensive set of helper functions for functional programming.for
loop to iterate over the array and extract the "type" property from each element.Pros and Cons
Other Considerations
Library Descriptions
map()
, filter()
, and reduce()
. It aims to provide a concise and consistent API for working with data.Special JS Feature/ Syntax
None mentioned in this benchmark, as it focuses on iterating over an array using different methods.