<script src="//cdnjs.cloudflare.com/ajax/libs/lodash-fp/0.10.4/lodash-fp.min.js"></script>
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
var pure = numbers.filter(num => num % 2 ===0).map(num=> ({
result: num
}))
_.flow(_.map(num => ({
result: num
})), _.filter(num => num %2 ===0))(numbers)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
pure js | |
lodash fp |
Test name | Executions per second |
---|---|
pure js | 2675404.8 Ops/sec |
lodash fp | 70196.3 Ops/sec |
Let's break down the provided JSON and explain what's being tested, along with the pros and cons of each approach.
Benchmark Definition
The benchmark is comparing two approaches: using pure JavaScript (without any libraries) versus using the Lodash functional programming library (lodash-fp
).
Script Preparation Code
This code creates an array numbers
containing 30 numbers from 1 to 30. This will be used as input for both test cases.
Html Preparation Code
This line includes a script tag that loads the lodash-fp.min.js
file, which is required for the Lodash functional programming library.
Individual Test Cases
There are two test cases:
: This test case uses pure JavaScript to filter and map the
numbers` array.: This test case uses the Lodash functional programming library (
lodash-fp) to filter and map the
numbers` array.lodash-fp
library), which may add overhead or complexity.Library: Lodash Functional Programming Library (lodash-fp
)
Lodash is a popular JavaScript utility library that provides a wide range of functions for various tasks, such as array manipulation, object transformation, and more. The functional programming version (_
) of Lodash (lodash-fp
) is a subset of the full library, optimized for use with functional programming techniques.
Other Considerations
When choosing between pure JavaScript and lodash fp
, consider the trade-offs:
lodash fp
might be a better choice due to its concise syntax.Alternatives
Other alternatives to consider for array filtering and mapping in pure JavaScript include:
Array.prototype.filter()
and Array.prototype.map()
methods directly (like in the "pure js" test case).Keep in mind that these alternatives may offer different trade-offs in terms of readability, performance, or complexity.