Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Pick vs compiled pick fn
(version: 0)
Comparing performance of:
Lodash vs compiled pick function
Created:
4 years 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:
Lodash
arr.map(function (element) { return _.pick( element, 'type', 'subtype', 'card_last4', 'card_type', 'card_exp_month', 'card_exp_year', 'card_country', 'something' ); });
compiled pick function
const props = [ "type", "subtype", "card_last4", "card_type", "card_exp_month", "card_exp_year", "card_country", "something" ]; function pickBuilder(props) { const obj = props.map((prop) => `if (has.call(el, '${prop}')) res.${prop} = el.${prop}`).join(';'); return new Function('el', `const has = Object.prototype.hasOwnProperty; const res = {}; ${obj}; return res;`); } const pick = pickBuilder(props) arr.map((element) => pick(element));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
compiled pick function
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):
I'll break down the provided benchmark definition and test cases to explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark is testing two approaches for picking specific properties from an object: using Lodash's `pick` function versus a compiled version of a pick function. **Options Compared** 1. **Lodash's `pick` function**: The official JavaScript utility library Lodash provides a `pick` function that takes an object, an array of keys to pick, and returns a new object with only the specified properties. 2. **Compiled pick function**: A custom-made function that dynamically generates a string representing the picked properties using template literals and then uses the `Function` constructor to create a new function that performs the picking. **Pros and Cons** **Lodash's `pick` function:** Pros: * Convenient and easy to use * Optimized for performance Cons: * Requires an external library (Lodash) * May not be suitable for all scenarios, as it doesn't handle cases where a key is not found in the object **Compiled pick function:** Pros: * Allows for more control over the generated code * Can potentially outperform Lodash's `pick` function if optimized correctly Cons: * Requires manual implementation and maintenance * May be less readable and maintainable due to its custom nature **Other Considerations** * The benchmark is running on Firefox 113, which might affect the results. * The test case uses a large array of objects (`100000`) to simulate real-world performance scenarios. **Library Used: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for various tasks, including data manipulation, string manipulation, and more. In this case, the `pick` function is used to select specific properties from an object. **Special JS Feature/Syntax** None mentioned in the provided benchmark definition. However, it's worth noting that the compiled pick function uses some advanced JavaScript features like template literals, `Function` constructor, and `has.call()` method, which might be less familiar to developers without experience with these concepts. **Alternatives** Other alternatives for picking properties from an object include: * Using a library like moment.js for date manipulation (not relevant in this case) * Implementing a custom function using JavaScript's built-in functions like `Object.prototype.hasOwnProperty()` * Utilizing ES6+ features like destructuring or the `Object.entries()` method Keep in mind that these alternatives might not be as convenient or performant as Lodash's `pick` function or the compiled pick function.
Related benchmarks:
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Lodash cloneDeep vs. Lodash clone vs. Array.slice() vs. Array.slice(0) vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign() vs Array.slice() vs Array.slice(0)
Array.prototype.slice vs Lodash drop
Comments
Confirm delete:
Do you really want to delete benchmark?