Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodas pick vs plain function
(version: 0)
Comparing performance of:
lodash omit vs plain Pick
Created:
3 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:
function flattenKeys(s) { return s.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '').split('.') } function pick2(obj, props) { let result = {}; if (obj == null || props == null || props.length == 0) { return undefined; } if (Array.isArray(obj)) { result = []; } if (typeof props == "string") { props = [props]; } if (obj != null && obj != undefined && typeof obj == 'object') { const newObj = structuredClone(obj); let tempObj = structuredClone(newObj); for (const i in props) { const path = flattenKeys(props[i]); let pathExists = false; for (const j in path) { if (tempObj[path[j]] != undefined) { tempObj = tempObj[path[j]]; if (j == (path.length - 1)) { pathExists = true; } } } if (pathExists) { tempObj = structuredClone(newObj); let partialRes = {}; if (tempObj[path[0]] != undefined) { tempObj = tempObj[path[0]]; if (typeof tempObj == 'object') { partialRes = {}; if (Array.isArray(tempObj)) { partialRes = []; } } if (path.length == 1) { partialRes[path[0]] = tempObj; } else if (path.length > 1) { partialRes[path[0]] = pick2(tempObj, [path.slice(1).join('.')]); } } Object.assign(result, partialRes); } } return result; } return obj; }; var testA = { a: { b: { c: { d: [{ sample1: 'sample1' }, { sample2: 'sample2' }], e: { f: 'other' } } } } };
Tests:
lodash omit
const resultA = _.pick(testA, ['a.b.c.d.e']); const resultB = _.pick(testA, ['a.b.c.d[0]']);
plain Pick
const resultA = pick2(testA, ['a.b.c.d.e']); const resultB = pick2(testA, ['a.b.c.d[0]']);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash omit
plain 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 dive into the explanation of the benchmark. **Benchmark Definition JSON** The provided JSON represents a JavaScript microbenchmarking test case named "lodas pick vs plain function". The benchmark is designed to compare two approaches: using Lodash's `pick` function and a custom implementation (named `pick2`) for extracting specific properties from an object. **Options Compared** Two options are compared: 1. **Lodash's `pick` function**: This method takes two arguments: the object to be processed (`testA`) and an array of property paths to extract (`['a.b.c.d.e']` or `['a.b.c.d[0]']`). The method returns a new object containing only the specified properties. 2. **Custom implementation (`pick2`)**: This function is designed to mimic Lodash's behavior but uses a different approach. It creates a deep clone of the original object, then iterates through the property paths to extract and merge the desired values. **Pros and Cons** * **Lodash's `pick` function**: + Pros: - Well-tested and optimized by a large community. - Provides a simple and concise API. - Cons: - Adds an external dependency (Lodash). - May not be suitable for all edge cases or complex object structures. * **Custom implementation (`pick2`)**: + Pros: - No external dependencies. - Can handle more complex object structures and edge cases. + Cons: - Less concise API compared to Lodash's `pick`. - Requires manual handling of property paths, which can lead to errors. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functional programming tools. In this benchmark, the `pick` function from Lodash is used to extract specific properties from an object. The library's purpose is to simplify common tasks and improve code readability. **Special JS Features/Syntax** In this benchmark, there are no special JavaScript features or syntax used beyond standard ECMAScript 2020 (ES12) features. **Alternative Approaches** Other approaches for extracting specific properties from an object might include: 1. **Using a property access chain**: `testA['a.b.c.d.e']` or `testA['a.b.c.d[0]']`. 2. **Utilizing a library like Moment.js** (not relevant to this benchmark). 3. **Implementing a custom recursive function** to traverse the object structure. However, Lodash's `pick` function and the custom implementation (`pick2`) are two distinct approaches that cater to different use cases and requirements. The choice between them ultimately depends on the specific needs of the project, personal preference, or desired level of complexity.
Related benchmarks:
hoek vs lodash
lodash omit vs native function
Lodash.get vs Lodash.property vs native test
Set (Lodash vs Lodash/fp vs Immutable) comp. test
Comments
Confirm delete:
Do you really want to delete benchmark?