<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var obj = Array(1000)
.fill()
.reduce((acc, curr, i) => {
acc[i] = i;
return acc;
}, {});
var predicate = v => v % 2 === 0;
_.pickBy(obj, predicate);
Object.entries(obj).filter(([_, v]) => predicate(v));
Object.entries(obj).reduce((acc, [k, v]) => {
if (predicate(v)) acc[k] = v;
return acc;
}, {});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.pickBy | |
filter | |
reduce |
Test name | Executions per second |
---|---|
_.pickBy | 13150.6 Ops/sec |
filter | 8764.8 Ops/sec |
reduce | 8368.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark for three different approaches: _.pickBy
(using Lodash), Object.entries + filter
, and Object.entries + reduce
. Our goal is to understand what's being tested, the pros and cons of each approach, and other considerations.
Benchmark Definition
The benchmark definition is a JavaScript code snippet that sets up an object obj
with 1000 properties and fills them with values from 0 to 999. A predicate function predicate
is also defined, which checks if a value is even (i.e., v % 2 === 0
). The goal is to filter or pick specific properties from the obj
based on this predicate.
Approaches
We have three approaches being compared:
pickBy
function, which takes an object and a callback function as arguments. It returns a new object with only the properties that pass the test implemented by the provided function.Array.prototype.filter()
method to create a new array with only the elements for which the predicate function returns true
. Then, it uses the Object.fromEntries()
method to convert the filtered array back into an object.filter()
, we use the Array.prototype.reduce()
method to create a new object with only the properties that pass the test implemented by the predicate function.Pros and Cons
Here's a brief summary of each approach:
filter()
, but with the added benefit of being able to accumulate values in the accumulator object (in case you need to do so).Other Considerations
Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object transformation, and functional programming. The pickBy
function in particular is designed to be fast and efficient while still being easy to use.
Special JS Feature/Syntax
None mentioned explicitly, but note that Object.fromEntries()
was introduced in ECMAScript 2015 (ES6) as a part of the standard library. It's a modern feature that allows you to create objects from arrays using a more concise syntax.
Alternatives
If you want to explore other alternatives, here are a few options:
true
. However, as mentioned earlier, it might have higher performance overhead compared to using Lodash's optimized implementation.Object.keys()
to get an array of property names and then use Array.prototype.reduce()
to create a new object with only the properties that pass the test implemented by the predicate function.