Skip to content

Commit

Permalink
perf(get): replaces reduce with for loop in hot function
Browse files Browse the repository at this point in the history
  • Loading branch information
stalniy committed Oct 17, 2020
1 parent 0dc832a commit e54d86a
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ export function isArrayAndNotNumericField<T>(object: T | T[], field: string): ob
}

function getField<T extends AnyObject>(object: T | T[], field: string, get: GetField) {
if (isArrayAndNotNumericField(object, field)) {
return object.reduce((acc, item) => {
const value = get(item, field);
return value !== undefined ? acc.concat(value) : acc;
}, []);
if (!isArrayAndNotNumericField(object, field)) {
return get(object, field);
}

return get(object, field);
let result: unknown[] = [];

for (let i = 0; i < object.length; i++) {
const value = get(object[i], field);
if (typeof value !== 'undefined') {
result = result.concat(value);
}
}

return result;
}

export function getValueByPath(object: AnyObject, field: string, get: GetField) {
Expand Down

0 comments on commit e54d86a

Please sign in to comment.