-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve deepEquals performance (#4292)
* Change fast-deep-equal to fast-equals * Add changelog and change deepEquals depscription * run cs-format * Add JSDocs for isFunctions and customDeepEqual * Update CHANGELOG.md --------- Co-authored-by: Heath C <51679588+heath-freenome@users.noreply.github.com>
- Loading branch information
1 parent
fc1c8c3
commit 514ea85
Showing
15 changed files
with
89 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,46 @@ | ||
import isEqualWith from 'lodash/isEqualWith'; | ||
import { createCustomEqual, State } from 'fast-equals'; | ||
|
||
/** Implements a deep equals using the `lodash.isEqualWith` function, that provides a customized comparator that | ||
/** Check if all parameters are typeof function. | ||
* | ||
* @param a - The first element to check typeof | ||
* @param b - The second element to check typeof | ||
* @returns - if typeof a and b are equal to function return true, otherwise false | ||
*/ | ||
function isFunctions(a: any, b: any) { | ||
return typeof a === 'function' && typeof b === 'function'; | ||
} | ||
|
||
/** Implements a deep equals using the `fast-equal.createCustomEqual` function, that provides a customized comparator that | ||
* assumes all functions in objects are equivalent. | ||
* | ||
* @param a - The first element to compare | ||
* @param b - The second element to compare | ||
* @returns - True if the `a` and `b` are deeply equal, false otherwise | ||
*/ | ||
const customDeepEqual = createCustomEqual({ | ||
createInternalComparator: (comparator: (a: any, b: any, state: State<any>) => boolean) => { | ||
return (a: any, b: any, _idxA: any, _idxB: any, _parentA: any, _parentB: any, state: State<any>) => { | ||
if (isFunctions(a, b)) { | ||
// Assume all functions are equivalent | ||
// see https://github.com/rjsf-team/react-jsonschema-form/issues/255 | ||
return true; | ||
} | ||
|
||
return comparator(a, b, state); | ||
}; | ||
}, | ||
}); | ||
|
||
/** Implements a deep equals using the `fast-equal.createCustomEqual` function, that provides a customized comparator that | ||
* assumes all functions are equivalent. | ||
* | ||
* @param a - The first element to compare | ||
* @param b - The second element to compare | ||
* @returns - True if the `a` and `b` are deeply equal, false otherwise | ||
*/ | ||
export default function deepEquals(a: any, b: any): boolean { | ||
return isEqualWith(a, b, (obj: any, other: any) => { | ||
if (typeof obj === 'function' && typeof other === 'function') { | ||
// Assume all functions are equivalent | ||
// see https://github.com/rjsf-team/react-jsonschema-form/issues/255 | ||
return true; | ||
} | ||
return undefined; // fallback to default isEquals behavior | ||
}); | ||
if (isFunctions(a, b)) { | ||
return true; | ||
} | ||
return customDeepEqual(a, b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters