<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;
}
let paths = {};
function lodashFnDynamicCachedArray(input, key) {
paths[key] = paths[key] || _.toPath(`contextMenu.data.${key}`);
const documents = _.get(input, paths[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');
const input = { contextMenu: { data: { documents: [{ id: 'doc' }] } } };
test(lodashFnDynamicCachedArray, 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 | |
lodash dynamic key using manually cached array |
Test name | Executions per second |
---|---|
destructuring with defaults, empty input | 158829328.0 Ops/sec |
lodash get, empty input | 13713576.0 Ops/sec |
destructuring with defaults, find item | 24543030.0 Ops/sec |
lodash get, find item | 8402243.0 Ops/sec |
destructuring dynamic key | 24512064.0 Ops/sec |
lodash dynamic key | 4614469.0 Ops/sec |
lodash dynamic key using array | 14741652.0 Ops/sec |
lodash dynamic key using manually cached array | 14933426.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's tested in this benchmark.
Benchmark Definition
The benchmark is designed to compare the performance of two approaches: ES6 destructuring and Lodash's _.get method, specifically when dealing with string and array keys. The tests cover various scenarios, including:
Options Compared
The benchmark compares the following options:
Both approaches have their own pros and cons:
Test Scenarios
The benchmark tests the following scenarios:
Results
The benchmark results show the number of executions per second for each test scenario on Chrome 129 running on a desktop device. The top-performing approach varies depending on the specific test scenario.
Keep in mind that these results are specific to Chrome 129 on a desktop device and may not reflect other browsers, devices, or performance characteristics.