<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() {
underscore.map(PromosArray, function (promo) {
return promo.type;
});
}
function testLodashMap() {
lodash.map(PromosArray, function (promo) {
return promo.type;
});
}
function testForLoop() {
var newArray = [];
for (var i = 0; i < PromosArray.length; i++) {
newArray.push(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 | 1265912.2 Ops/sec |
testArrayMap | 1507532.5 Ops/sec |
testUnderscoreMap | 1907867.9 Ops/sec |
testLodashMap | 1977808.5 Ops/sec |
testForLoop | 1285028.4 Ops/sec |
Let's break down what's being tested in this benchmark.
What is being tested?
The test cases are designed to compare the performance of different approaches for mapping over an array of objects. The array contains three objects with different types: "regular", "markdown", and "promo".
Options compared
There are four options being compared:
map()
method: This is a built-in method in JavaScript that maps over an array, applying a specified function to each element.map()
method: Underscore.js is a utility library for functional programming in JavaScript. Its map()
method is similar to the native map()
method but provides additional features and convenience methods.map()
method: Lodash.js is another utility library for functional programming in JavaScript. Its map()
method is similar to Underscore.js' map()
method but offers more advanced functions and options.Pros and Cons of each approach
map()
method:map()
method:map()
method.map()
method:Other considerations
Library descriptions
Special JS feature or syntax
None mentioned in the benchmark.
Alternatives
Other alternatives to these three approaches could include:
map()
to create a new array, you can use reduce()
to accumulate the results in an existing array.forEach()
to iterate over the array and apply the transformation.Note that these alternatives may have different performance characteristics or trade-offs compared to using map()
or other functional programming methods.