<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(predicate)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.pickBy | |
native filter |
Test name | Executions per second |
---|---|
_.pickBy | 30055.9 Ops/sec |
native filter | 6217.7 Ops/sec |
Let's dive into the world of performance benchmarking with MeasureThat.net.
Benchmark Definition
The provided JSON represents a benchmark that compares the performance of two approaches: Lodash's _.pickBy()
function and native JavaScript's Object.entries().filter()
method. The benchmark is designed to measure the speed of each approach in extracting specific properties from an object based on a predicate function.
Options Compared
Two options are compared:
_pickBy()
function: This function takes two arguments, object
and predicate
, where object
is the source object, and predicate
is a function that returns true
for the desired properties. The function iterates through the properties of the object using a for...in
loop and includes only the properties for which the predicate function returns true
.Object.entries().filter()
method: This approach uses the entries()
method to iterate over an object's key-value pairs, converting it into an array of arrays. The filter()
method is then applied to this array, allowing you to specify a callback function that filters out unwanted properties.Pros and Cons
Here are some pros and cons for each approach:
_pickBy()
function:Object.entries().filter()
method:Library and Syntax Considerations
In this benchmark, Lodash's _.pickBy()
function is used. The _pickBy
method is a utility function in Lodash that extracts specific properties from an object based on a predicate function. This library provides additional utility functions for array and object manipulation, among others.
There are no special JavaScript features or syntax used in this benchmark. It relies solely on standard JavaScript language features.
Alternatives
For native JavaScript implementations, you can also use other approaches like:
Object.keys()
to get an array of keys, then using Array.prototype.filter()
to filter out unwanted properties.Object.getOwnPropertyNames()
or Object.getOwnPropertySymbols()
(depending on the object type) to iterate over object properties directly.Keep in mind that each approach has its own performance characteristics and trade-offs. MeasureThat.net helps you compare these different methods under controlled conditions, allowing for a more accurate assessment of their relative performance.