<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script>
function destructuringFn(input) {
const { contextMenu: { data: { documents = [] } = {} } = {} } = input;
return documents;
}
function destructuringFnDynamic(input, key) {
const { contextMenu: { data: { [key]: documents = [] } = {} } = {} } = input;
return documents;
}
function lodashFn(input) {
const documents = _.get(input, 'contextMenu.data.documents', []);
return documents;
}
function lodashFnDynamic(input, key) {
const documents = _.get(input, `contextMenu.data.${key}`, []);
return documents;
}
function lodashFnDynamicArray(input, key) {
const documents = _.get(input, ['contextMenu', 'data', 'key'], []);
return documents;
}
function test(fn, input, key) {
fn(input, key);
}
test(destructuringFn, {});
test(lodashFn, {});
const input = { contextMenu: { data: { documents: [{ id: 'doc' }] } } };
test(destructuringFn, input);
const input = { contextMenu: { data: { documents: [{ id: 'doc' }] } } };
test(lodashFn, input);
const input = { contextMenu: { data: { documents: [{ id: 'doc' }] } } };
test(destructuringFn, input, 'documents');
const input = { contextMenu: { data: { documents: [{ id: 'doc' }] } } };
test(lodashFnDynamic, input, 'documents');
const input = { contextMenu: { data: { documents: [{ id: 'doc' }] } } };
test(lodashFnDynamicArray, input, 'documents');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
destructuring with defaults, empty input | |
lodash get, empty input | |
destructuring with defaults, find item | |
lodash get, find item | |
destructuring dynamic key | |
lodash dynamic key | |
lodash dynamic key using array |
Test name | Executions per second |
---|---|
destructuring with defaults, empty input | 6074671.0 Ops/sec |
lodash get, empty input | 1839597.0 Ops/sec |
destructuring with defaults, find item | 1672529.9 Ops/sec |
lodash get, find item | 983559.8 Ops/sec |
destructuring dynamic key | 1690493.6 Ops/sec |
lodash dynamic key | 769004.6 Ops/sec |
lodash dynamic key using array | 1224656.9 Ops/sec |
I'll provide an explanation of the benchmark, options compared, pros and cons, and other considerations.
Benchmark Overview
MeasureThat.net is testing the performance of three approaches for accessing nested object properties in JavaScript:
Options Compared
The benchmark compares three options for accessing nested object properties:
destructuringFn
) and one without ( destructuringFnDynamic
)lodashFn
), one using dynamic keys with dot notation (lodashFnDynamic
), and one using dynamic keys with array notation (lodashFnDynamicArray
)Pros and Cons of Each Approach
Pros:
Cons:
Pros:
Cons:
This option is likely used as a baseline or warm-up test, ensuring that the environment and benchmarking setup are correct before running the actual tests.
Other Considerations
The benchmark also considers various factors that may affect performance, such as:
By considering these factors and options, MeasureThat.net provides a comprehensive benchmarking scenario that helps developers understand the performance characteristics of their JavaScript code.