Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash pick vs native pick vs compiled fn pick vs native pick (own) vs compiled (own) v2
(version: 0)
Comparing performance of:
Lodash vs native pick vs compiled function vs native pick (own) vs compiled (own)
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 pick3(obj, keys) { const hasOwn = Object.prototype.hasOwnProperty; var res = {}; if (typeof keys === 'string') { if (hasOwn.call(obj, keys)) { res[keys] = obj[keys]; } return res; } var len = keys.length; var idx = -1; while (++idx < len) { var key = keys[idx]; if (hasOwn.call(obj, key)) { res[key] = obj[key]; } } return res; }; var pick4 = new Function('el', 'if (!el) return {}; const hasOwn = Object.prototype.hasOwnProperty; const res = {}; ' + 'if (hasOwn.call(el, "a")) res.a = el.a;' + 'if (hasOwn.call(el, "d")) res.d = el.d;' + 'if (hasOwn.call(el, "i")) 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']))
native pick (own)
data.map((obj) => pick3(obj, ['a','d','i']))
compiled (own)
data.map((obj) => pick4(obj, ['a','d','i']))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Lodash
native pick
compiled function
native pick (own)
compiled (own)
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):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, including the provided benchmark. The benchmark tests four different approaches for creating a subset of an object's properties using the `pick` function: 1. **Lodash**: Uses the Lodash library to implement the `pick` function. 2. **Native Pick (own)**: Implements the `pick` function from scratch without any external libraries. 3. **Compiled Function**: Compiles a custom JavaScript function that implements the `pick` logic. 4. **Compiled (own) v2**: An updated version of the compiled function approach, possibly with performance optimizations. **Options Compared** The benchmark compares the performance of each approach: * Lodash: Uses the existing `pick` function from Lodash library * Native Pick (own): Implements the logic manually without any external libraries * Compiled Function: Compiles a custom JavaScript function that implements the `pick` logic * Compiled (own) v2: An updated version of the compiled function approach **Pros and Cons** Here are some pros and cons for each approach: 1. **Lodash**: * Pros: Fast, reliable, and well-tested implementation from a reputable library. * Cons: Requires an external dependency, might not be optimal for performance. 2. **Native Pick (own)**: * Pros: No external dependencies, potentially optimized for performance. * Cons: Implementation must be carefully crafted to avoid performance issues. 3. **Compiled Function**: * Pros: Customizable, can include performance optimizations specific to the use case. * Cons: Requires manual implementation and optimization, might not be as efficient as native implementations. 4. **Compiled (own) v2**: * Pros: Optimized for performance compared to previous compiled function approach. * Cons: Still requires careful implementation and optimization. **Benchmark Results** The latest benchmark results show that: 1. Native Pick (own) outperforms the other approaches with an Execution Per Second (EPS) value of 549.93505859375. 2. Compiled Function has a slightly lower EPS value, at 482.97705078125. 3. Lodash and Compiled (own) v2 have significantly lower EPS values, at 76.40721893310547 and 336.81103515625, respectively. Keep in mind that these results are specific to this benchmark and may not generalize to other use cases or environments.
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
Lodash pick vs native pick vs compiled fn pick vs native pick (own) vs compiled (own)
Comments
Confirm delete:
Do you really want to delete benchmark?