Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Chain vs Native (with &&) vs Native (without &&) vs Flow
(version: 5)
Comparing performance of:
Chain vs Native (with &&) vs Native (without &&) vs Flow
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<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>
Script Preparation code:
var fp = _.noConflict(); var data = Array(10000000).fill({ a: 'a', b: 1 });
Tests:
Chain
_.chain(data).filter(f => f.a === 'a').filter(f => f.b === 1).filter(f => f.b + 1 === 2).map(f => f.a).filter(f => f === 'a').value()
Native (with &&)
data.filter(f => f.a === 'a' && f.b === 1 && f.b + 1 === 2).map(f => f.a).filter(f => f === 'a')
Native (without &&)
data.filter(f => f.a === 'a').filter(f => f.b === 1).filter(f => f.b + 1 === 2).map(f => f.a).filter(f => f === 'a')
Flow
fp.flow( fp.filter(f => f.a === 'a'), fp.filter(f => f.b === 1), fp.filter(f => f.b + 1 === 2), fp.map(f => f.a), fp.filter(f => f === 'a') )(data)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Chain
Native (with &&)
Native (without &&)
Flow
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Chain
0.6 Ops/sec
Native (with &&)
1.1 Ops/sec
Native (without &&)
0.6 Ops/sec
Flow
0.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided benchmark compares the performance of three different approaches to filter and map data in Lodash: 1. **Lodash Chain**: Uses the `_.chain()` method to create a chain of functions that manipulate the data. 2. **Native (with &&)**: Uses the logical AND operator (`&&`) to combine conditions for filtering data. 3. **Native (without &&)**: Similar to the previous one, but without using the logical AND operator. **Library and Purpose** The library used in this benchmark is Lodash, a popular JavaScript utility library that provides a wide range of functions for manipulating data, working with strings, and more. In the benchmark code, `_.noConflict()` is used to prevent conflicts between the original Lodash library and its fp (functional programming) version. This is done by creating a new instance of Lodash (`fp`) instead of using the existing one. **Special JS Features or Syntax** There are no special JavaScript features or syntax mentioned in this benchmark, except for the use of `_.chain()` to create a chain of functions, which is a common pattern in functional programming. The logical AND operator (`&&`) is used to combine conditions, but it's not a special feature. **Pros and Cons of Different Approaches** 1. **Lodash Chain**: This approach provides a clear separation of concerns, making the code easier to read and maintain. However, it can be slower due to the overhead of creating a chain of functions. 2. **Native (with &&)**: This approach is often faster than the Lodash chain because it avoids the overhead of function creation. However, it requires more explicit conditionals, which can make the code harder to read and maintain. 3. **Native (without &&)**: Similar to the previous one, but without using the logical AND operator. This approach provides a balance between readability and performance. **Other Considerations** * The benchmark is run on a Chrome browser with a specific version and platform, which may not be representative of other browsers or platforms. * The benchmark only runs four iterations per test case, which may not provide a comprehensive view of the performance characteristics of each approach. * The use of `_.noConflict()` to prevent conflicts between Lodash versions is not necessary in this benchmark. **Alternatives** If you're looking for alternatives to Lodash or want to explore other approaches to filtering and mapping data, here are some options: 1. **Ramda**: A functional programming library that provides a similar set of functions to Lodash. 2. **Immutable.js**: A library that provides immutable data structures and functions for working with them. 3. **ES6 Iterators**: You can use the built-in `for...of` loop or `Array.prototype.forEach()` method to iterate over arrays and objects, which can be a more lightweight alternative to using a library like Lodash. Keep in mind that each approach has its own strengths and weaknesses, and the choice of library or technique depends on your specific use case and performance requirements.
Related benchmarks:
Lodash Chain vs Native (with &&) vs Native (without &&) vs Flow with random data
Lodash Chain (with &&) vs Native (with &&) vs Native (without &&) vs Flow
Lodash Chain vs Native vs Flow
Lodash Chain vs Native (with &&) vs Native (without &&) vs Flow, with actual filtering
Comments
Confirm delete:
Do you really want to delete benchmark?