Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.pickBy vs native.reduce2
(version: 1)
Comparing performance of:
pickby vs native
Created:
2 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 Preparation code:
var getDate = (index) => { var sdate = new Date('2023-01-01') var tdate = new Date(sdate.getTime() + 86400000 * index) return `${tdate.getFullYear()}-${tdate.getMonth() + 1}-${tdate.getDate()}` } var dailySettings = Array(752) .fill({}) .reduce((acc, curr, i) => { acc = { ...acc, [getDate(i)]: { 1: 100, 2: 110, extraAdult: 50, extraChild: 50, algorithm: 'DBR', baseRateCode: 'RATE-1', baseAmount: 110 } }; return acc; }, {}); var predicate = (v, k) => (k === '2025-1-1') var predicate2 = v => v === '2025-1-1'
Tests:
pickby
_.pickBy(dailySettings, predicate);
native
Object.keys(dailySettings).reduce((acc, cur) => { if (predicate2(cur)) { acc = { ...acc, [cur]: dailySettings[cur] } } return acc; }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
pickby
native
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark compares two approaches to filter an object: `_.pickBy` from Lodash and a native implementation using `Object.keys()` and `reduce()`. The test case involves creating an array of objects with varying properties and filtering them based on a predicate function. **What's being tested?** 1. **_.pickBy**: This is a utility function from the Lodash library that filters an object by a predicate function, returning only the keys with a truthy value. 2. **Native implementation**: A custom implementation using `Object.keys()` and `reduce()` to achieve the same filtering effect. **Comparison of approaches** Both methods filter the objects in the `dailySettings` array based on the `predicate` function. However, they differ in their approach: 1. **_.pickBy**: * Pros: + Concise and readable code. + Lodash provides a well-maintained and widely used library for utility functions. * Cons: + Adds an external dependency (Lodash) to the benchmark. + Might incur additional overhead due to the function call and object lookup. 2. **Native implementation**: * Pros: + No external dependencies or overhead. + Native JavaScript, so no performance implications for modern browsers. * Cons: + Code can be more verbose and harder to read. + Requires a good understanding of the `Object.keys()` and `reduce()` methods. **Other considerations** 1. **Optimization**: Lodash's `_.pickBy` is likely optimized for performance, as it has been extensively tested and reviewed by the community. 2. **Browser support**: The benchmark runs on Chrome 114 and Mac OS X 10.15.7, which might not be representative of all browsers or platforms. 3. **Future-proofing**: It's essential to keep in mind that JavaScript and browser implementations can evolve over time, affecting performance characteristics. **Library used: Lodash** Lodash is a popular JavaScript library for utility functions. `_pickBy` is a part of the Lodash collection, providing a concise way to filter objects based on a predicate function. **Special JS feature or syntax** None mentioned in this benchmark explanation. However, if you'd like to know about other special features or syntax, feel free to ask! **Alternatives** If you're looking for alternatives to Lodash's `_.pickBy`, consider using: 1. **Built-in JavaScript methods**: As seen in the native implementation, using `Object.keys()` and `reduce()` can achieve similar results. 2. **Other libraries**: Other libraries like Ramda or Mithril provide similar utility functions for filtering objects. 3. **Custom implementations**: Depending on your specific requirements, you might need to create a custom implementation that suits your needs. Keep in mind that the choice of library or implementation ultimately depends on your project's specific needs and constraints.
Related benchmarks:
days in month
instanceof vs regular comparison
fight to the death - for loop vs chained array methods (10,000,000) minus sort
lodash groupBy vs Array.reduce 100k with gey generation 2
Comments
Confirm delete:
Do you really want to delete benchmark?