Skip to content

Commit

Permalink
Replace isArraySorted with keys.every(everyKeyInOrder).
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Oct 2, 2023
1 parent c2a8e34 commit d0aa030
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/utilities/common/canonicalStringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ function stableObjectReplacer(key: string, value: any) {
// prevents needlessly rearranging the indices of arrays.
if (proto === Object.prototype || proto === null) {
const keys = Object.keys(value);
if (isArraySorted(keys)) return value;
// If keys is already sorted, let JSON.stringify serialize the original
// value instead of creating a new object with keys in the same order.
if (keys.every(everyKeyInOrder)) return value;
const unsortedKey = JSON.stringify(keys);
let sortedKeys = sortingMap.get(unsortedKey);
if (!sortedKeys) {
Expand All @@ -71,11 +73,14 @@ function stableObjectReplacer(key: string, value: any) {
return value;
}

function isArraySorted(keys: readonly string[]): boolean {
for (let k = 1, len = keys.length; k < len; ++k) {
if (keys[k - 1] > keys[k]) {
return false;
}
}
return true;
// Since everything that happens in stableObjectReplacer benefits from being as
// efficient as possible, we use a static function as the callback for
// keys.every in order to test if the provided keys are already sorted without
// allocating extra memory for a callback.
function everyKeyInOrder(
key: string,
i: number,
keys: readonly string[]
): boolean {
return i === 0 || keys[i - 1] <= key;
}

0 comments on commit d0aa030

Please sign in to comment.