Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash pick vs native pick vs compiled fn pick
(version: 0)
Comparing performance of:
Lodash vs native pick vs compiled function
Created:
4 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 src=''> </script>
Script Preparation code:
function pick1(obj, keys) { var res = {}; if (typeof keys === 'string') { if (keys in obj) { res[keys] = obj[keys]; } return res; } var len = keys.length; var idx = -1; while (++idx < len) { var key = keys[idx]; if (key in obj) { res[key] = obj[key]; } } return res; }; var pick2 = new Function('el', 'if (!el) return {}; const res = {}; ' + 'if ("a" in el) res.a = el.a;' + 'if ("d" in el) res.d = el.d;' + 'if ("i" in el) res.i = el.i;' + ' return res;') function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive } var data = []; for (let i = 0; i < 10000; i++) { const obj = {}; const propsCount = getRandomInt(0, 10); for (let j = 0; j < propsCount; j++) { const propCode = getRandomInt(97, 122); obj[String.fromCharCode(propCode)] = propCode; } data.push(obj); }
Tests:
Lodash
data.map((obj) => _.pick(obj, ['a','d','i']))
native pick
data.map((obj) => pick1(obj, ['a','d','i']))
compiled function
data.map((obj) => pick2(obj, ['a','d','i']))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash
native pick
compiled function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
106.9 Ops/sec
native pick
802.7 Ops/sec
compiled function
665.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what's being tested on MeasureThat.net. **Benchmark Purpose** The benchmark measures the performance of three different approaches to pick (extract) specific properties from an object: 1. **Lodash**: Uses the popular JavaScript library Lodash, which provides a utility function `pick` that can extract specific properties from an object. 2. **Native Pick**: Implements the `pick` functionality in pure JavaScript, without using any external libraries. 3. **Compiled Function**: Compiles a JavaScript function that performs the same task as the native pick implementation. **Options Compared** The benchmark compares the performance of these three approaches: * Lodash: Uses the `pick` function from Lodash to extract properties. * Native Pick: Implements the `pick` functionality in pure JavaScript, without using any external libraries. * Compiled Function: Compiles a JavaScript function that performs the same task as the native pick implementation. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: * **Lodash**: Pros: + Fast and efficient + Provides a well-maintained and widely-used library + Easy to use and integrate * Cons: + Requires an external library, which can add overhead * **Native Pick**: Pros: + No additional libraries or dependencies required + Optimized for performance in pure JavaScript + Customizable and extensible * Cons: + May require more code and effort to implement correctly * **Compiled Function**: Pros: + Can be optimized for performance using Just-In-Time (JIT) compilation + Allows for more control over the generated code + Can be used in situations where native pick is not available or suitable * Cons: + May require more expertise and effort to implement correctly + Can be slower than native pick due to JIT compilation overhead **Library Used** The benchmark uses Lodash version 4.17.5, which provides a robust and widely-used implementation of the `pick` function. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. The focus is on comparing the performance of three different approaches to extract properties from an object. **Alternative Approaches** Other alternatives for extracting properties from an object include: * Using a library like Moment.js for date and time parsing * Using a library like jQuery for DOM manipulation * Using a library like React or Angular for building UI components In terms of pure JavaScript implementations, other approaches might involve using `Object.keys()`, `Array.prototype.forEach()`, or `Array.prototype.reduce()` to iterate over the properties and extract the desired ones. Overall, this benchmark provides a useful comparison of three different approaches to extracting properties from an object, highlighting the trade-offs between using external libraries, native JavaScript implementations, and compiled functions.
Related benchmarks:
pick first key of objecT: lodash vs Object.keys vs for...in...break
Lodash pick vs es pick vs native version 2
Lodash pick vs native pick vs compiled fn pick vs native pick (own) vs compiled (own)
Lodash pick vs native pick vs compiled fn pick vs native pick (own) vs compiled (own) v2
Comments
Confirm delete:
Do you really want to delete benchmark?