Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.pickBy vs native vs ES6 Filter
(version: 0)
Comparing performance of:
Lodash pickBy vs Native Filter vs ES6 Filter
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {}; for (let i = 0; i < 1000; i++) { const randomNum = Math.floor(Math.random() * 4); obj[i.toString()] = randomNum === 0 ? "undefined" : randomNum === 1 ? false : true; }
Tests:
Lodash pickBy
_.pickBy(obj);
Native Filter
function pickBy(object) { const obj = {}; for (const key in object) { if (object[key]) { obj[key] = object[key]; } } return obj; } pickBy(obj)
ES6 Filter
Object.keys(obj).filter(key => { if (obj[key]) return obj[key] })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash pickBy
Native Filter
ES6 Filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 132 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash pickBy
11826.5 Ops/sec
Native Filter
31693.4 Ops/sec
ES6 Filter
45308.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Definition** The benchmark is testing three approaches to filter an object: `_.pickBy` (a function from the Lodash library), native JavaScript filter, and ES6 filter. **Options Compared** 1. **Lodash _.pickBy**: This function takes two arguments - the object to be filtered (`object`) and a function that determines whether each property should be included in the output. The implementation uses a `for...in` loop to iterate over the object's properties, checking if the property value is truthy. 2. **Native JavaScript Filter**: This approach uses the built-in `Array.prototype.filter()` method to filter an array of keys. The implementation assumes that the object has been converted to an array of its own key-value pairs. 3. **ES6 Filter**: This approach uses the `Object.keys()` and `Array.prototype.filter()` methods to filter the object's properties. **Pros and Cons** 1. **Lodash _.pickBy**: * Pros: Well-tested, widely supported, and optimized for performance. It is also a more concise way of filtering an object. * Cons: Requires Lodash library to be included in the test code, which may add overhead to the benchmark. 2. **Native JavaScript Filter**: * Pros: Fastest approach, as it leverages the built-in `Array.prototype.filter()` method. It also doesn't require any additional libraries or overhead. * Cons: Requires the object to be converted to an array of its own key-value pairs, which may introduce extra overhead for large objects. 3. **ES6 Filter**: * Pros: Easy to implement and understand, as it uses well-known methods from the ECMAScript standard. * Cons: May not be as optimized or performance-critical as the native filter approach. **Library Used** The Lodash library is used in the `_.pickBy` function. It provides a convenient way to perform common tasks, such as filtering objects, without having to write custom code. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax beyond what's standard in modern JavaScript implementations (ECMAScript 2022+). **Alternatives** If you want to explore alternative approaches to filtering an object, here are a few options: * Using `Object.fromEntries()` and `Array.prototype.filter()`: ```javascript const obj = Object.fromEntries(Object.entries(obj).filter(([key, value]) => value)); ``` * Using a custom implementation with `for...in` loop and conditional statements: ```javascript function filterObject(obj) { const result = {}; for (const key in obj) { if (obj[key]) { result[key] = obj[key]; } } return result; } ``` Keep in mind that these alternatives may not be as optimized or efficient as the native JavaScript filter approach. In summary, the benchmark is testing three approaches to filtering an object: Lodash _.pickBy, native JavaScript filter, and ES6 filter. The native filter approach is likely the fastest due to its use of built-in methods, while Lodash _.pickBy provides a more concise and convenient way to perform the task.
Related benchmarks:
Lodash vs Native Filters : BabbleTech 02
pickby vs filter on object
pickby vs filter on object keys only
Array.prototype.some() vs. Filter vs. Array.prototype.indexOf()
Comments
Confirm delete:
Do you really want to delete benchmark?