<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/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'
);
});
arr.map(function (element) {
const {
type,
subtype,
card_last4,
card_type,
card_exp_month,
card_exp_year,
card_country,
something
} = element;
return {
type,
subtype,
card_last4,
card_type,
card_exp_month,
card_exp_year,
card_country,
something
};
});
const props = [
"type",
"subtype",
"card_last4",
"card_type",
"card_exp_month",
"card_exp_year",
"card_country",
"something"
];
arr.map(function (element) {
const res = {};
for(let prop of props){
res[prop] = element[prop]
}
return res;
});
arr.map(function (element) {
return {
type: element.type,
subtype: element.subtype,
card_last4: element.card_last4,
card_type: element.card_type,
card_exp_month: element.card_exp_month,
card_exp_year: element.card_exp_year,
card_country: element.card_country,
something: element.something
};
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Native | |
Manual pick with property names | |
Manual pick |
Test name | Executions per second |
---|---|
Lodash | 11.4 Ops/sec |
Native | 1086.1 Ops/sec |
Manual pick with property names | 82.1 Ops/sec |
Manual pick | 986.0 Ops/sec |
Let's break down the benchmark and explain what is being tested.
What is being tested?
The benchmark is comparing three approaches for extracting specific properties from an object:
pick
function (Lodash
)Native
)Manual pick
) and (Manual pick with property names
)Options compared
pick
function: This is a utility function from the popular JavaScript library Lodash that allows you to specify which properties to include in an object.Pros and Cons
pick
function:Library usage
In the benchmark, Lodash is used as a library to provide the pick
function. The script preparation code includes importing the Lodash library using a CDN link (<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
). This library is not included in the Chrome browser by default, so it's likely that this benchmark is running on a machine with access to the Internet.
Special JS feature
The benchmark uses native destructuring syntax (Native
), which was introduced in ECMAScript 2018 (ES2018). While most modern browsers support ES2018 features, it's worth noting that older browsers may not.