<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).reduce((acc, [k, v]) => {
if (predicate(v)) acc[k] = v;
return acc;
}, {});
Object.fromEntries(Object.entries(obj).filter(([k, v]) => predicate(v)));
function pickBy(object, predicate) {
const obj = {};
for (const key in object) {
if (object[key] && predicate(object[key])) {
obj[key] = object[key];
}
}
return obj;
}
pickBy(obj, predicate);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.pickBy | |
native | |
soy | |
iterate keys |
Test name | Executions per second |
---|---|
_.pickBy | 18099.1 Ops/sec |
native | 8056.2 Ops/sec |
soy | 8000.9 Ops/sec |
iterate keys | 33236.8 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Overview
The benchmark compares three approaches to filter an object in JavaScript:
_.pickBy(obj, predicate)
(using Lodash library)Object.entries(obj).reduce((acc, [k, v]) => { ... }, {})
)function pickBy(object, predicate) { ... }
)Library: Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks like filtering, mapping, and reducing data structures.
In this benchmark, the _.pickBy(obj, predicate)
function is used to filter an object based on a predicate (a function that takes one argument and returns a boolean value). The predicate function in this case checks if the value of each property is even (v % 2 === 0
).
Native JavaScript Implementation
The native implementation uses Object.entries(obj)
to get an array of key-value pairs from the object, then reduces it using the reduce()
method. The callback function takes two arguments: [k, v]
, which represents each key-value pair in the array. If the predicate function returns true for a value, the corresponding key is added to a new object (acc[k] = v;
).
Manual Iteration
The custom implementation uses a for...in
loop to iterate over the properties of the object. It checks if each property value meets the predicate condition and adds it to a new object (obj[key] = object[key];
) if true.
Comparison Overview
Here's a brief summary of each approach:
Object.entries(obj).reduce((acc, [k, v]) => { ... }, {})
): Uses built-in JavaScript functions for filtering, which can be optimized by the engine.function pickBy(object, predicate) { ... }
): Uses a custom implementation to iterate over properties, which can provide control but may also lead to performance issues if not optimized correctly.Considerations
When choosing an approach:
_.pickBy(obj, predicate)
if you're comfortable with Lodash and don't mind adding dependency.Object.entries(obj).reduce((acc, [k, v]) => { ... }, {})
) might be a better choice.function pickBy(object, predicate) { ... }
) can be suitable.Other Alternatives
Some other JavaScript libraries or approaches that could be used for filtering include:
Array.prototype.filter()
: Can be used on arrays instead of objects.Array.prototype.map()
with Object.fromEntries()
and Array.prototype.filter()
: Provides a more functional programming style solution.