<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.fp.min.js"></script>
var data = Array(1000000).fill({ filtering: true, mapping: 42 });
data
.filter(({ filtering }) => filtering)
.map(({ mapping }) => mapping)
_.pipe(
_.filter(({ filtering }) => filtering),
_.map(({ mapping }) => mapping)
)(data)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native filter-map | |
Lodash FP filter-map |
Test name | Executions per second |
---|---|
Native filter-map | 5.6 Ops/sec |
Lodash FP filter-map | 18.3 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Overview
The benchmark compares two approaches to filter and map an array of objects:
filter
and map
methods)_.pipe()
methodOptions Compared
Two options are being compared:
filter
and map
methods._
.pipe()` method.Pros and Cons of Each Approach
Pros:
Cons:
Pros:
Cons:
Library and Purpose
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like data manipulation, string processing, and more. The FP API is part of this library and offers a concise way to compose functions together using _.pipe()
.
Special JS Feature/Syntax
No special JavaScript features or syntax are being used in these benchmarks. They only rely on standard JavaScript language features.
Alternatives
Other alternatives for filtering and mapping arrays include:
stream
module.Keep in mind that the choice of implementation depends on the specific use case and requirements. In some cases, native code might be preferred for its speed, while Lodash FP or other libraries might be chosen for their expressiveness and flexibility.