<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr = [];
var object = {
type: 'aaa',
subtype: 'bbb',
card_last4:'bbb',
card_type:'bbb',
card_exp_month:'bbb',
card_exp_year:'bbb',
card_country:'bbb',
foo: 'bar'
};
for (var i = 0; i <= 100000; i++) { arr.push(object); }
arr.map(function (element) {
return _.pick(
element,
'type',
'subtype',
'card_last4',
'card_type',
'card_exp_month',
'card_exp_year',
'card_country',
'something'
);
});
function pick(object, keys) {
return keys.reduce((obj, key) => {
if (object && object.hasOwnProperty(key)) {
obj[key] = object[key];
}
return obj;
}, {});
}
arr.map(function (element) {
return pick(
element,
['type',
'subtype',
'card_last4',
'card_type',
'card_exp_month',
'card_exp_year',
'card_country',
'something']
);
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash pick | |
Native pick |
Test name | Executions per second |
---|---|
Lodash pick | 5.3 Ops/sec |
Native pick | 31.9 Ops/sec |
Let's break down the provided JSON benchmark.
Benchmark Definition
The benchmark is designed to compare the performance of two approaches: using Lodash's pick
function and implementing a native pick
function.
Script Preparation Code
section defines an array arr
containing 100,000 objects with various properties. Each object has a fixed set of properties that will be used for the comparison.Html Preparation Code
section includes the link to Lodash's JavaScript library version 4.17.5.Individual Test Cases
There are two test cases:
pick
function to extract specific properties from each object in the array.Test Name
indicates that this is a test case for Lodash's pick
function.pick
function to achieve similar results as Lodash's implementation.Test Name
indicates that this is a test case for the native implementation of pick
.Library: Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, object manipulation, and more. In this benchmark, Lodash's pick
function is used to extract specific properties from objects.
pick
function is to efficiently select a subset of properties from an object while preserving the rest.Native Implementation
The native implementation of pick
is a custom function that achieves similar results as Lodash's implementation.
pick
functionDevice Platform, Browser, and Operating System
The latest benchmark result shows that Chrome 117 on a Mac OS X 10.15.7 device platform achieved better performance for both test cases.
Alternatives
Other alternatives for benchmarking JavaScript performance include:
These alternatives offer a range of options for benchmarking JavaScript performance, depending on the specific requirements and focus areas.