Skip to content

Commit

Permalink
Revert "switch lodash.isEqual to fast-equals.deepEqual to improve per…
Browse files Browse the repository at this point in the history
…formance"

This reverts commit 86570fe.
  • Loading branch information
igorbrasileiro committed Jan 9, 2025
1 parent 86570fe commit 9a606ae
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 58 deletions.
10 changes: 0 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ should change the heading of the (upcoming) version to include a major version b
-->

# 5.25.0

## @rjsf/utils

- Switched uses of `lodash.isEqual()` to `fast-equals.deepEqual()` in many utility functions.

## @rjsf/validator-ajv8

- Switched uses of `lodash.isEqual()` to `fast-equals.deepEqual()` at precompiledValidator.

# 5.24.0

## @rjsf/core
Expand Down
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"react": "^16.14.0 || >=17"
},
"dependencies": {
"fast-equals": "^5.2.1",
"json-schema-merge-allof": "^0.8.1",
"jsonpointer": "^5.0.1",
"lodash": "^4.17.21",
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/enumOptionsDeselectValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepEqual } from 'fast-equals';
import isEqual from 'lodash/isEqual';

import { EnumOptionsType, RJSFSchema, StrictRJSFSchema } from './types';
import enumOptionsValueForIndex from './enumOptionsValueForIndex';
Expand All @@ -22,7 +22,7 @@ export default function enumOptionsDeselectValue<S extends StrictRJSFSchema = RJ
): EnumOptionsType<S>['value'] | EnumOptionsType<S>['value'][] | undefined {
const value = enumOptionsValueForIndex<S>(valueIndex, allEnumOptions);
if (Array.isArray(selected)) {
return selected.filter((v) => !deepEqual(v, value));
return selected.filter((v) => !isEqual(v, value));
}
return deepEqual(value, selected) ? undefined : selected;
return isEqual(value, selected) ? undefined : selected;
}
6 changes: 3 additions & 3 deletions packages/utils/src/enumOptionsIsSelected.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepEqual } from 'fast-equals';
import isEqual from 'lodash/isEqual';

import { EnumOptionsType, RJSFSchema, StrictRJSFSchema } from './types';

Expand All @@ -13,7 +13,7 @@ export default function enumOptionsIsSelected<S extends StrictRJSFSchema = RJSFS
selected: EnumOptionsType<S>['value'] | EnumOptionsType<S>['value'][]
) {
if (Array.isArray(selected)) {
return selected.some((sel) => deepEqual(sel, value));
return selected.some((sel) => isEqual(sel, value));
}
return deepEqual(selected, value);
return isEqual(selected, value);
}
6 changes: 3 additions & 3 deletions packages/utils/src/parser/ParserValidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import get from 'lodash/get';
import { deepEqual } from 'fast-equals';
import isEqual from 'lodash/isEqual';

import { ID_KEY } from '../constants';
import hashForSchema from '../hashForSchema';
Expand Down Expand Up @@ -67,7 +67,7 @@ export default class ParserValidator<T = any, S extends StrictRJSFSchema = RJSFS
const existing = this.schemaMap[key];
if (!existing) {
this.schemaMap[key] = identifiedSchema;
} else if (!deepEqual(existing, identifiedSchema)) {
} else if (!isEqual(existing, identifiedSchema)) {
console.error('existing schema:', JSON.stringify(existing, null, 2));
console.error('new schema:', JSON.stringify(identifiedSchema, null, 2));
throw new Error(
Expand All @@ -91,7 +91,7 @@ export default class ParserValidator<T = any, S extends StrictRJSFSchema = RJSFS
* @throws - Error when the given `rootSchema` differs from the root schema provided during construction
*/
isValid(schema: S, _formData: T, rootSchema: S): boolean {
if (!deepEqual(rootSchema, this.rootSchema)) {
if (!isEqual(rootSchema, this.rootSchema)) {
throw new Error('Unexpectedly calling isValid() with a rootSchema that differs from the construction rootSchema');
}
this.addSchema(schema, hashForSchema<S>(schema));
Expand Down
8 changes: 4 additions & 4 deletions packages/utils/src/parser/schemaParser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import forEach from 'lodash/forEach';
import isEqual from 'lodash/isEqual';

import { FormContextType, RJSFSchema, StrictRJSFSchema } from '../types';
import { ITEMS_KEY, PROPERTIES_KEY } from '../constants';
import { PROPERTIES_KEY, ITEMS_KEY } from '../constants';
import ParserValidator, { SchemaMap } from './ParserValidator';
import { resolveAnyOrOneOfSchemas, retrieveSchemaInternal } from '../schema/retrieveSchema';
import { deepEqual } from 'fast-equals';
import { retrieveSchemaInternal, resolveAnyOrOneOfSchemas } from '../schema/retrieveSchema';

/** Recursive function used to parse the given `schema` belonging to the `rootSchema`. The `validator` is used to
* capture the sub-schemas that the `isValid()` function is called with. For each schema returned by the
Expand All @@ -24,7 +24,7 @@ function parseSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends
) {
const schemas = retrieveSchemaInternal<T, S, F>(validator, schema, rootSchema, undefined, true);
schemas.forEach((schema) => {
const sameSchemaIndex = recurseList.findIndex((item) => deepEqual(item, schema));
const sameSchemaIndex = recurseList.findIndex((item) => isEqual(item, schema));
if (sameSchemaIndex === -1) {
recurseList.push(schema);
const allOptions = resolveAnyOrOneOfSchemas<T, S, F>(validator, schema, rootSchema, true);
Expand Down
16 changes: 7 additions & 9 deletions packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import get from 'lodash/get';
import isEmpty from 'lodash/isEmpty';

import {
ALL_OF_KEY,
ANY_OF_KEY,
CONST_KEY,
DEFAULT_KEY,
DEPENDENCIES_KEY,
ONE_OF_KEY,
PROPERTIES_KEY,
ONE_OF_KEY,
REF_KEY,
ALL_OF_KEY,
} from '../constants';
import findSchemaDefinition from '../findSchemaDefinition';
import getClosestMatchingOption from './getClosestMatchingOption';
Expand All @@ -34,8 +34,8 @@ import isSelect from './isSelect';
import retrieveSchema, { resolveDependencies } from './retrieveSchema';
import isConstant from '../isConstant';
import { JSONSchema7Object } from 'json-schema';
import isEqual from 'lodash/isEqual';
import optionsList from '../optionsList';
import { deepEqual } from 'fast-equals';

const PRIMITIVE_TYPES = ['string', 'number', 'integer', 'boolean', 'null'];

Expand Down Expand Up @@ -129,7 +129,8 @@ function maybeAddDefaultToObject<T = any>(
if (!isEmpty(computedDefault)) {
obj[key] = computedDefault;
}
} // Else store computedDefault if it's a non-empty object(e.g. not {}) and satisfies certain conditions
}
// Else store computedDefault if it's a non-empty object(e.g. not {}) and satisfies certain conditions
// Condition 1: If computedDefault is not empty or if the key is a required field
// Condition 2: If the parent object is required or emptyObjectFields is not 'populateRequiredDefaults'
else if (
Expand Down Expand Up @@ -270,10 +271,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
experimental_dfsb_to_compute?.constAsDefaults === 'skipOneOf'
) {
// If we are in a oneOf of a primitive type, then we want to pass constAsDefaults as 'never' for the recursion
experimental_dfsb_to_compute = {
...experimental_dfsb_to_compute,
constAsDefaults: 'never',
};
experimental_dfsb_to_compute = { ...experimental_dfsb_to_compute, constAsDefaults: 'never' };
}
schemaToCompute = oneOf![
getClosestMatchingOption<T, S, F>(
Expand Down Expand Up @@ -379,7 +377,7 @@ export function ensureFormDataMatchingSchema<
let validFormData: T | T[] | undefined = formData;
if (isSelectField) {
const getOptionsList = optionsList(schema);
const isValid = getOptionsList?.some((option) => deepEqual(option.value, formData));
const isValid = getOptionsList?.some((option) => isEqual(option.value, formData));
validFormData = isValid ? formData : undefined;
}

Expand Down
13 changes: 5 additions & 8 deletions packages/utils/src/schema/retrieveSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from 'lodash/get';
import isEqual from 'lodash/isEqual';
import set from 'lodash/set';
import times from 'lodash/times';
import transform from 'lodash/transform';
Expand All @@ -14,10 +15,10 @@ import {
ANY_OF_KEY,
DEPENDENCIES_KEY,
IF_KEY,
ITEMS_KEY,
ONE_OF_KEY,
PROPERTIES_KEY,
REF_KEY,
PROPERTIES_KEY,
ITEMS_KEY,
} from '../constants';
import findSchemaDefinition, { splitKeyElementFromObject } from '../findSchemaDefinition';
import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema';
Expand All @@ -33,7 +34,6 @@ import {
ValidatorType,
} from '../types';
import getFirstMatchingOption from './getFirstMatchingOption';
import { deepEqual } from 'fast-equals';

/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and dependencies
* resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData` that is used to do the
Expand Down Expand Up @@ -256,10 +256,7 @@ export function resolveSchema<T = any, S extends StrictRJSFSchema = RJSFSchema,
)
);
const allPermutations = getAllPermutationsOfXxxOf<S>(allOfSchemaElements);
return allPermutations.map((permutation) => ({
...schema,
allOf: permutation,
}));
return allPermutations.map((permutation) => ({ ...schema, allOf: permutation }));
}
// No $ref or dependencies or allOf attribute was found, returning the original schema.
return [schema];
Expand Down Expand Up @@ -359,7 +356,7 @@ export function resolveAllReferences<S extends StrictRJSFSchema = RJSFSchema>(
};
}

return deepEqual(schema, resolvedSchema) ? schema : resolvedSchema;
return isEqual(schema, resolvedSchema) ? schema : resolvedSchema;
}

/** Creates new 'properties' items for each key in the `formData`
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/schema/toIdSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import get from 'lodash/get';
import { deepEqual } from 'fast-equals';
import isEqual from 'lodash/isEqual';

import { ALL_OF_KEY, DEPENDENCIES_KEY, ID_KEY, ITEMS_KEY, PROPERTIES_KEY, REF_KEY } from '../constants';
import isObject from '../isObject';
Expand Down Expand Up @@ -42,7 +42,7 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
): IdSchema<T> {
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
const _schema = retrieveSchema<T, S, F>(validator, schema, rootSchema, formData, experimental_customMergeAllOf);
const sameSchemaIndex = _recurseList.findIndex((item) => deepEqual(item, _schema));
const sameSchemaIndex = _recurseList.findIndex((item) => isEqual(item, _schema));
if (sameSchemaIndex === -1) {
return toIdSchemaInternal<T, S, F>(
validator,
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/schema/toPathSchema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { deepEqual } from 'fast-equals';
import get from 'lodash/get';
import isEqual from 'lodash/isEqual';
import set from 'lodash/set';

import {
ADDITIONAL_PROPERTIES_KEY,
ALL_OF_KEY,
ANY_OF_KEY,
ADDITIONAL_PROPERTIES_KEY,
DEPENDENCIES_KEY,
ITEMS_KEY,
NAME_KEY,
Expand Down Expand Up @@ -50,7 +50,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
): PathSchema<T> {
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
const _schema = retrieveSchema<T, S, F>(validator, schema, rootSchema, formData, experimental_customMergeAllOf);
const sameSchemaIndex = _recurseList.findIndex((item) => deepEqual(item, _schema));
const sameSchemaIndex = _recurseList.findIndex((item) => isEqual(item, _schema));
if (sameSchemaIndex === -1) {
return toPathSchemaInternal<T, S, F>(
validator,
Expand Down
1 change: 0 additions & 1 deletion packages/validator-ajv8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"dependencies": {
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"fast-equals": "^5.2.1",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21"
},
Expand Down

0 comments on commit 9a606ae

Please sign in to comment.