<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
var fp = _.noConflict();
var data = Array(10000000).fill({ a: Math.random() < 0.5 ? 'a' : 'b', b: Math.random() * 100000 });
_.chain(data).filter(f => f.a === 'a').filter(f => f.b > 50000).filter(f => f.b + 1 === 2).map(f => f.a).filter(f => f === 'a').value()
data.filter(f => f.a === 'a' && f.b > 50000 && f.b + 1 === 2).map(f => f.a).filter(f => f === 'a')
data.filter(f => f.a === 'a').filter(f => f.b > 50000).filter(f => f.b + 1 === 2).map(f => f.a).filter(f => f === 'a')
fp.flow(
fp.filter(f => f.a === 'a'),
fp.filter(f => f.b > 50000),
fp.filter(f => f.b + 1 === 2),
fp.map(f => f.a),
fp.filter(f => f === 'a')
)(data)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Chain | |
Native (with &&) | |
Native (without &&) | |
Flow |
Test name | Executions per second |
---|---|
Chain | 17.9 Ops/sec |
Native (with &&) | 6.8 Ops/sec |
Native (without &&) | 6.7 Ops/sec |
Flow | 8.0 Ops/sec |
Let's break down the provided benchmark and explain what is tested, compared options, pros and cons, library usage, special JavaScript features or syntax, and other considerations.
Benchmark Definition:
The benchmark measures the performance of three different approaches to filter and map data:
chain
method to create a pipeline of methods that are applied to the data in sequence.&&
) to combine multiple conditions in a single line of code.flow
method to create a pipeline of methods that are applied to the data in sequence.Script Preparation Code:
The script preparation code creates an array of 10 million objects with two properties each (a
and b
). The values of these properties are randomly generated, with a
being either 'a' or 'b', and b
being a random number between 0 and 100000.
Html Preparation Code:
The HTML preparation code includes two external JavaScript files:
Individual Test Cases:
Each test case defines a separate benchmark that applies one of the four approaches to filter and map the data.
Here's what each approach does:
chain
method returns an object with a value()
method, which is called to execute the final operation.&&
). This approach can be more efficient than the Lodash pipeline because it avoids the overhead of function calls and object creation. However, it may be less readable and maintainable for complex operations.&&
approach due to the additional overhead introduced by the separate conditionals.flow
method instead of chaining methods.Library Usage:
The benchmark uses two external libraries:
Lodash is a popular utility library for JavaScript that provides functional programming utilities, including chain
and flow
. The fp module, which includes only functional programming utilities, is likely used to isolate these specific features from other parts of the library.
Special JavaScript Features or Syntax:
There are no special JavaScript features or syntax used in this benchmark. However, it does demonstrate the use of logical operators (&&
) for conditional statements, as well as function call chains and pipelines.
Pros and Cons of Different Approaches:
Here's a brief summary of the pros and cons of each approach:
&&
approach due to additional overhead.Other Considerations:
Alternatives:
In conclusion, this benchmark provides a valuable comparison of four different approaches to filtering and mapping data: Lodash Chain, Native (with &&), Native (without &&), and Flow. By understanding the pros and cons of each approach, developers can choose the most suitable method for their specific use case and performance requirements.