<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
function format_lodash(data) {
return data.map(({ id, label, properties}) => {
return { id, label, properties: _.mapValues(properties, v => v[0]) };
});
}
function format_pure(data) {
return data.map(({ id, label, properties}) => {
for (let [key, value] of Object.entries(properties)) { properties[key] = value[0] }
return { id, label, properties};
});
}
const input = [{
id: 1,
label: 'label 1',
property_1: ['prop_1'],
property_2: ['prop_2']
},
{
id: 2,
label: 'label 2',
property_3: ['prop_3'],
property_4: ['prop_4']
}
]
console.log(format_lodash(input))
const input = [{
id: 1,
label: 'label 1',
property_1: ['prop_1'],
property_2: ['prop_2']
},
{
id: 2,
label: 'label 2',
property_3: ['prop_3'],
property_4: ['prop_4']
}
]
console.log(format_lodash(input))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash Test | |
Pure JS Test |
Test name | Executions per second |
---|---|
Lodash Test | 72058.3 Ops/sec |
Pure JS Test | 70588.2 Ops/sec |
Benchmark Overview
The provided JSON represents two JavaScript microbenchmarks: Lodash MapValue vs Pure JS
and its two test cases, Lodash Test
and Pure JS Test
. The benchmarks aim to compare the performance of Lodash's mapValues
function with a pure JavaScript implementation.
What is being tested?
The test cases provide input data in the following format:
[
{
id: 1,
label: 'label 1',
property_1: ['prop_1'],
property_2: ['prop_2']
},
{
id: 2,
label: 'label 2',
property_3: ['prop_3'],
property_4: ['prop_4']
}
]
The mapValues
function is used to transform the properties of each object in the array. The benchmark compares the performance of two implementations:
mapValues
function, which is a part of the Lodash library.Object.entries()
and for...of
loop to achieve similar results.Options compared
The options being compared are:
mapValues
function vs. a pure JavaScript implementationPros and Cons of different approaches
mapValues
function:mapValues
function, which can lead to errors or inefficiencies.Library and syntax used
The Lodash library is used in the first test case. Specifically, the mapValues
function is used to transform the properties of each object in the input array.
There are no special JavaScript features or syntax used in this benchmark. However, note that the use of Object.entries()
and for...of
loop in the pure JavaScript implementation demonstrates modern JavaScript language features.
Alternatives
If you want to write similar benchmarks for other JavaScript libraries or functions, consider the following alternatives:
Keep in mind that the choice of library and implementation will depend on the specific benchmarking goals and requirements.