<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var data = Array(10000000).fill({ a: 'a', b: 1 });
_.chain(data).filter(f => f.a === 'a').filter(f => f.b === 1).map(f => f.a).value()
data.filter(f => f.a === 'a' && f.b === 1).map(f => f.a)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Chain | |
Without chain |
Test name | Executions per second |
---|---|
Chain | 3.6 Ops/sec |
Without chain | 4.5 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what is being tested.
Benchmark Definition
The benchmark definition json contains two main components:
a
and b
. The data
variable is assigned this value.Test Cases
The benchmark definition contains two individual test cases:
**: This test case uses the Lodash
chain()function to create a pipeline of operations on the
data` array._.chain(data).filter(f => f.a === 'a')
) filters out elements where a
is not equal to 'a'
._.filter(f => f.b === 1)
) filters out elements where b
is not equal to 1
._.map(f => f.a)
) maps each element to its value for the property a
..value()
method returns the result of the entire pipeline.data.filter(f => f.a === 'a' && f.b === 1)
) filters out elements where a
is not equal to 'a'
and b
is not equal to 1
._.map(f => f.a)
is incorrect here, it should be .map(f => f.a)
) maps each element to its value for the property a
.Options Compared
The benchmark compares two approaches:
data
array.Pros and Cons
Library Usage
The benchmark uses Lodash version 4.17.5. Lodash is a popular JavaScript library that provides a wide range of utility functions for tasks such as:
In this case, Lodash's chain()
function enables the use of a pipeline approach to filter and transform data in a more readable way.
Special JS Feature or Syntax
There are no special JavaScript features or syntax used in these test cases.