<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 | 68897296.0 Ops/sec |
lodash get, empty input | 3063274.5 Ops/sec |
destructuring with defaults, find item | 18837910.0 Ops/sec |
lodash get, find item | 2160528.5 Ops/sec |
destructuring null input | 109657968.0 Ops/sec |
lodash null input | 53679824.0 Ops/sec |
destructuring undefined input | 111116376.0 Ops/sec |
lodash undefined input | 55228308.0 Ops/sec |
destructuring dynamic key | 19644854.0 Ops/sec |
lodash dynamic key | 2055996.5 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what is being tested, compared, and their pros and cons.
Benchmark Definition:
The benchmark compares the performance of two approaches:
const { prop } = obj;
.Script Preparation Code:
The script preparation code defines four functions:
destructuringFn(input)
: A static version of the destructuring function, which assumes the input object has a specific structure.destructuringFnDynamic(input, key)
: A dynamic version of the destructuring function, which extracts a value from an object using a dynamically provided key.lodashFn(input)
: The Lodash _.get function applied to the input object.lodashFnDynamic(input, key)
: The Lodash _.get function applied to the input object with a dynamic key.Html Preparation Code:
The HTML preparation code includes the Lodash library.
Test Cases:
The test cases execute each of the four functions on different input scenarios and measure their execution time. The input scenarios cover various cases, such as:
Comparison:
The benchmark compares the performance of each function across different input scenarios. It measures the execution time for each approach and provides the results in a table format.
Pros and Cons:
Here's a brief analysis of the pros and cons of each approach:
Conclusion:
The benchmark provides a comprehensive comparison of two approaches to extracting values from objects. The results suggest that:
By understanding the strengths and weaknesses of each approach, developers can choose the best fit for their specific use case.