Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash pick vs native pick
(version: 1)
Comparing Lodash _.pick with a native implementation.
Comparing performance of:
_.pick vs native
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
Script Preparation code:
var arr = []; var object = { type: 'aaa', subtype: 'bbb', card_last4:'bbb', card_type:'bbb', card_exp_month:'bbb', card_exp_year:'bbb', card_country:'bbb', foo: 'bar' }; for (var i = 0; i <= 100000; i++) { arr.push(object); }
Tests:
_.pick
arr.map(el => _.pick(el, ['type', 'subtype', 'card_last4', 'card_type', 'card_exp_month', 'card_exp_year', 'card_country']));
native
function pick(object, keys) { return keys.reduce((obj, key) => { if (object && object.hasOwnProperty(key)) { obj[key] = object[key]; } return obj; }, {}); } arr.map(el => pick(el, ['type', 'subtype', 'card_last4', 'card_type', 'card_exp_month', 'card_exp_year', 'card_country']));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.pick
native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.pick
10.7 Ops/sec
native
59.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you're looking at compares two different methods for extracting specific properties from an array of objects: one using the Lodash library’s `_.pick` function and the other using a native JavaScript implementation. ### Options Compared 1. **Lodash `_.pick`:** This method leverages the Lodash library to efficiently extract a given set of properties from each object in the array. 2. **Native Implementation:** This method defines a custom function called `pick`, which mimics the behavior of `_.pick` using pure JavaScript. ### Pros and Cons #### Lodash `_.pick` - **Pros:** - **Simplicity and Readability:** Using Lodash is often clearer and more concise, improving readability for those familiar with the library. - **Performance Optimization:** Lodash functions are often optimized for performance, which can make them faster in many scenarios. - **Cons:** - **Dependency:** It requires loading an external library, which can increase bundle size and add complexity to the project, especially if Lodash is not already being used elsewhere. - **Overhead:** If only a few utility functions from Lodash are needed, it may be considered overkill to include the entire library. #### Native Implementation - **Pros:** - **No External Dependencies:** A native implementation doesn’t require additional libraries, reducing the overall footprint of the application. - **Control and Customization:** Developers can easily tweak the implementation to suit specific needs. - **Cons:** - **Complexity:** Custom implementations can be more verbose and less intuitive than using a well-tested library function. - **Potential Performance Issues:** While in this benchmark the native implementation outperformed Lodash, it's not guaranteed that this will always be the case across different scenarios or datasets. Native methods could be less optimized for various edge cases. ### Other Considerations - **Performance Metrics:** The benchmark results reveal that the native implementation executed significantly more times per second (approximately 59.87 executions per second) compared to the Lodash implementation (around 10.67 executions per second). This indicates that, for this specific case and dataset (100,000 identical objects), the native implementation is far more efficient. - **Use Cases:** While Lodash may be preferred in projects with extensive use of utility functions, for lightweight data manipulation, especially when you only need a specific function once or twice, a native approach may be preferable. ### Alternatives Besides using Lodash or a custom function like this, other alternatives to consider include: - **ES6 Destructuring:** Simplifies the syntax when you want to create new objects with a subset of properties. However, it may not scale as well for deep nesting or dynamic keys. - **Object.keys() or Object.entries():** These methods allow more granular control over property extraction but require additional code for filtering keys. - **Typescript:** If type safety is a concern, using TypeScript's features can help manage data transformations while enforcing types. In conclusion, this benchmark serves to illustrate the trade-offs between utilizing a popular utility library versus a native approach, providing performance insights that can guide developers in optimizing their code.
Related benchmarks:
Lodash Pick vs Native destructuring vs Manual Picks
Lodash Pick vs Native destructuring vs Manual
Lodash Pick vs Native destructuring vs Manual Picks vs pick fn
Lodash Pick vs compiled pick fn
Lodash Pick vs compiled pick fn vs loop pick fn
Lodash Pick vs Native destructuring vs Manual Picks1
Lodash pick and native method
Lodash pick and native method 1
Lodash pick vs native
Comments
Confirm delete:
Do you really want to delete benchmark?