Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash pick and native method 1
(version: 0)
Comparing performance of:
Lodash pick vs Native pick
Created:
2 years ago
by:
Guest
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 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 <= 10; i++) { arr.push(object); }
Tests:
Lodash pick
arr.map(function (element) { return _.pick( element, 'type', 'subtype', 'card_last4', 'card_type', 'card_exp_month', 'card_exp_year', 'card_country', 'something' ); });
Native pick
function pick(object, keys) { return keys.reduce((obj, key) => { if (object && object.hasOwnProperty(key)) { obj[key] = object[key]; } return obj; }, {}); } arr.map(function (element) { return pick( element, ['type', 'subtype', 'card_last4', 'card_type', 'card_exp_month', 'card_exp_year', 'card_country', 'something'] ); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash pick
Native pick
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 provided benchmark and its explanations. **Benchmark Overview** The benchmark tests two approaches for filtering an object using JavaScript: 1. **Lodash `pick` method**: A popular utility library that provides a convenient way to extract specific properties from an object. 2. **Native `pick` function**: A native implementation of the same functionality, built into modern browsers. **Options Compared** The benchmark compares the performance of two options: * Lodash's `pick` method * Native JavaScript's `pick` function **Pros and Cons of Each Approach** ### Lodash `pick` Method Pros: * Easy to use: The API is simple and intuitive. * Flexibility: Can handle any number of properties being extracted. Cons: * Overhead: Loading an external library adds latency and increases the size of the benchmark execution. * Complexity: Understanding how to use it effectively requires knowledge of Lodash's API. ### Native `pick` Function Pros: * Built-in: No additional overhead from loading a library. * Lightweight: No extra dependencies or complexity. Cons: * Limited documentation: Not well-documented, making it harder for users to understand its usage. * Verbosity: Requires manual handling of the function call and property iteration. **Library Description** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, object manipulation, and more. The `pick` method allows you to extract specific properties from an object while ignoring others. In this benchmark, Lodash's `pick` method is used to filter the `object` structure and extract only certain properties (e.g., `type`, `subtype`, etc.). **Special JS Features or Syntax** The test cases use a special JavaScript feature: **Closure**. In the code snippet, `map()` function returns an array of new functions that are created when each iteration of the loop completes. These functions capture the current value of `element` in their scope and then call the `pick()` method on it. This is possible due to JavaScript's closure behavior, which allows variables from outer scopes (like `i`) to be accessible within inner functions (the new function created for each iteration). **Other Alternatives** In addition to Lodash's `pick` method and native JavaScript's `pick` function, there might be other libraries or implementations that offer similar functionality. Some alternatives could include: * Other utility libraries like `underscore.js`, `moment.js`, or `fast-json-pick` * Custom-built solutions using a specific property iteration technique * Browser-specific APIs (e.g., `Chrome's chrome.experimental.pick()`) However, it's worth noting that Lodash's `pick` method is one of the most well-known and widely used alternatives, so it's likely to be the primary focus in such benchmarks.
Related benchmarks:
circleTest
CircleSmallTest
lodash vs es6 in find method
lodash slice
Lodash IsEmpty Test
Comments
Confirm delete:
Do you really want to delete benchmark?