Test name | Executions per second |
---|---|
destructuring with defaults, empty input | 7554722.0 Ops/sec |
lodash get, empty input | 2090201.0 Ops/sec |
destructuring with defaults, find item | 2540010.5 Ops/sec |
lodash get, find item | 1181248.0 Ops/sec |
destructuring dynamic key | 2423492.5 Ops/sec |
lodash dynamic key | 911605.6 Ops/sec |
lodash dynamic key using array | 1607810.4 Ops/sec |
lodash dynamic key using manually cached array | 1588290.6 Ops/sec |
<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');