Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
pickby vs filter on object
(version: 0)
Comparing performance of:
pickby vs filter
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:
var obj = {} for (let i = 0; i < 1000; i++) { obj[String(i)] = Math.floor(Math.random() * 6) + 1 } var predicate = v => v % 2 === 0;
Tests:
pickby
_.pickBy(obj, predicate);
filter
Object.keys(obj).filter((e) => predicate(e))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
pickby
filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pickby
24177.4 Ops/sec
filter
89163.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, along with the pros and cons of each approach. **Benchmark Overview** The test case compares two approaches for filtering an object: `_.pickBy` from Lodash and `Object.keys().filter()`. Both methods are used to filter an object where the key-value pairs need to be retained only if a condition is met. In this benchmark, the condition is that the value should be odd (`v % 2 !== 0`). **Lodash _.pickBy** The Lodash library provides a function called `_.pickBy()` which takes two arguments: an object and a predicate function. The predicate function is applied to each key in the object, and if it returns `true`, the corresponding value is included in the resulting object. Pros: * More concise and expressive way of filtering objects * Often faster than vanilla JavaScript approaches due to Lodash's optimized implementation Cons: * Requires including an external library (in this case, Lodash) * May not be as well-known or understood by developers without a strong background in functional programming or Lodash **Vanilla JavaScript Object.keys().filter()** The `Object.keys()` method returns an array of the object's keys, which can then be filtered using the `Array.prototype.filter()` method. Pros: * No external libraries are required * Widely supported and understood by developers * Can be useful for learning or demonstrating basic JavaScript concepts Cons: * Often slower than Lodash-based approaches due to the overhead of creating an array and filtering it * More verbose code can lead to readability issues **Other Considerations** When comparing these two approaches, consider the following factors: * Readability: How easy is it to understand the intent behind each approach? Which one requires more boilerplate code or is more concise? * Maintainability: Which approach is easier to maintain and update if the filtering logic changes? * Performance: In what scenarios will one approach outperform the other? **Library Explanation** In this benchmark, Lodash is used as a library to provide the `_.pickBy()` function. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array and object manipulation, and more. **Special JS Feature/Syntax** There are no special JavaScript features or syntax being tested in this benchmark. However, it's worth noting that the `String(i)` function call is used to create keys for the object, which may be less readable than using a simple numeric index. In modern JavaScript, you might prefer to use an array of numbers instead: `[0, 1, ..., 999]`. **Alternatives** Other alternatives for filtering objects in JavaScript include: * Using a custom function with `Object.keys()` and `Array.prototype.filter()` * Utilizing libraries like Ramda or Lodash's sister project, UglifyJS * Leveraging modern JavaScript features like arrow functions or destructuring For this specific benchmark, if you wanted to exclude the external library dependency, you could rewrite the `_.pickBy` test case using a custom function and `Object.keys()`: ```javascript const obj = {}; for (let i = 0; i < 1000; i++) { obj[String(i)] = Math.floor(Math.random() * 6) + 1; } function filter(obj, predicate) { const keys = Object.keys(obj); return keys.filter(key => predicate(parseInt(key))); } const result = filter(obj, v => v % 2 !== 0); ``` This implementation uses the `Object.keys()` method to get an array of keys and then filters it using a custom `filter` function.
Related benchmarks:
_.pickBy vs _.filter on array
pickby vs filter on object keys only
_.pickBy vs native vs ES6 Filter
_.pickBy vs native Object.entries + filter
Comments
Confirm delete:
Do you really want to delete benchmark?