<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 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);
test(destructuringFn, null);
test(lodashFn, null);
let input;
test(destructuringFn, input);
let input;
test(lodashFn, input);
const input = { contextMenu: { data: { documents: [{ id: 'doc' }] } } };
test(destructuringFn, input, 'documents');
const input = { contextMenu: { data: { documents: [{ id: 'doc' }] } } };
test(lodashFn, 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 null input | |
lodash null input | |
destructuring undefined input | |
lodash undefined input | |
destructuring dynamic key | |
lodash dynamic key |
Test name | Executions per second |
---|---|
destructuring with defaults, empty input | 16394662.0 Ops/sec |
lodash get, empty input | 2589685.0 Ops/sec |
destructuring with defaults, find item | 14848909.0 Ops/sec |
lodash get, find item | 2355672.8 Ops/sec |
destructuring null input | 0.0 Ops/sec |
lodash null input | 9018978.0 Ops/sec |
destructuring undefined input | 0.0 Ops/sec |
lodash undefined input | 8901846.0 Ops/sec |
destructuring dynamic key | 13892219.0 Ops/sec |
lodash dynamic key | 2296507.8 Ops/sec |
Benchmark Overview
The provided benchmark compares the performance of two JavaScript approaches: ES6 destructuring and Lodash's get()
method. The test cases cover different scenarios, including accessing nested properties, handling null and undefined inputs, and optimizing for empty input.
ES6 Destructuring
ES6 destructuring is a shorthand syntax for extracting values from objects or arrays into variables. In this benchmark, the ES6 destructuring approach uses the following syntax:
const { prop1, prop2 } = obj;
This syntax allows for concise and readable code to access nested properties.
Lodash's get()
Method
Lodash's get()
method is a utility function that helps retrieve values from objects or arrays. In this benchmark, the Lodash get()
method is used with the following syntax:
const value = _.get(obj, 'path.to.prop1,prop2');
This syntax allows for flexible and dynamic path specification to access nested properties.
Performance Comparison
The benchmark results show that ES6 destructuring outperforms Lodash's get()
method in most cases, especially when accessing nested properties. However, Lodash's get()
method excels when handling null or undefined inputs, as it provides a safer and more explicit way to access values.
Here are some key takeaways from the benchmark results:
get()
method.get()
method is slower when accessing nested properties but provides better performance handling null or undefined inputs.Advice
Based on the benchmark results, consider the following advice:
get()
method when:Ultimately, the choice between ES6 destructuring and Lodash's get()
method depends on the specific use case, personal preference, and coding style.