Skip to content

Commit

Permalink
perf: use imperative loops when building merged result of records
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Sep 17, 2021
1 parent 17a92e1 commit b36f7bc
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/deepmerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,30 @@ function mergeRecords<
U extends DeepMergeMergeFunctionUtils,
MF extends DeepMergeMergeFunctionsURIs
>(values: Ts, utils: U) {
const neverValue = {};
return Object.fromEntries(
[...getKeys(values)]
.map((key) => {
const propValues = values
.map((value) =>
objectHasProperty(value, key) ? value[key] : neverValue
)
.filter((value) => value !== neverValue);
const result: Record<RecordProperty, unknown> = {};

// assert(propValues.length > 0);
/* eslint-disable functional/no-loop-statement, functional/no-conditional-statement -- using a loop here is more performant. */

if (propValues.length === 1) {
return [key, propValues[0]];
}
for (const key of getKeys(values)) {
const propValues = [];

return [key, mergeUnknowns(propValues, utils)];
})
.filter((value): value is [unknown, unknown] => value !== neverValue)
) as DeepMergeRecordsDefaultHKT<Ts, MF>;
for (const value of values) {
if (objectHasProperty(value, key)) {
propValues.push(value[key]);
}
}

// assert(propValues.length > 0);

result[key] =
propValues.length === 1
? propValues[0]
: mergeUnknowns(propValues, utils);
}

/* eslint-enable functional/no-loop-statement, functional/no-conditional-statement */

return result as DeepMergeRecordsDefaultHKT<Ts, MF>;
}

/**
Expand Down

0 comments on commit b36f7bc

Please sign in to comment.