-
-
Notifications
You must be signed in to change notification settings - Fork 14
Description
Here are the 2 performance enhancement we can do in jsonSchemaErrors
1. Localization.forLocale() reads from disk on every call
Every invocation of jsonSchemaErrors calls Localization.forLocale(), which reads the translation .ftl file from disk, parses it even when the language has not changed between calls.
export const jsonSchemaErrors = async (errorOutput, schemaUri, instance, options = {}) => {
const normalizedErrors = await normalizedOutput(instance, errorOutput, schemaUri);
const rootInstance = Instance.fromJs(instance);
const localization = await Localization.forLocale(options.language ?? "en-US");
return await getErrors(normalizedErrors, rootInstance, localization);
};
Solution: implement caching with simple Map for localization such that disk read occurs only once.
2. Instance.fromJs() called twice for the same input
Instance.fromJs(instance) is called once inside normalizedOutput and again in the outer jsonSchemaErrors function. Both calls receive the same instance value, meaning the conversion to AST is performed twice unnecessarily on every call.
Solution: call Instance.fromJs() once in jsonSchemaErrors and pass it into normalizedOutput instead of letting it recreate it again.