Skip to content

Commit

Permalink
fix(performance): Analyse and fix performance deopt in node.js (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Miszczyszyn authored Jan 17, 2021
1 parent a9dc2b8 commit c60a2a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
11 changes: 0 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ import type { AnySchema } from './types';
export const isDate = (d: unknown): d is Date =>
Object.prototype.toString.call(d) === '[object Date]';

export const fromEntries = (entries: readonly (readonly [string | number, any])[]) => {
const obj: Record<keyof any, any> = {};
// eslint-disable-next-line functional/no-loop-statement
for (let i = 0; i < entries.length; ++i) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
obj[entries[i]![0]] = entries[i]![1];
}

return obj;
};

type Modifier<S1, S2> = (arg: S1) => S2;
type SchemaArg<S> = (() => S) | S;
// prettier-ignore
Expand Down
26 changes: 15 additions & 11 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ValidationError } from './errors';
import { parse } from './parse';
import type { AnySchema, TypeOf } from './types';
import { fromEntries, isDate } from './utils';
import { isDate } from './utils';
import {
LITERAL_VALIDATOR,
STRING_VALIDATOR,
Expand Down Expand Up @@ -81,8 +81,10 @@ export const validate = <S extends AnySchema>(schema: S) => (value: unknown): Ty

const validatorValues = Object.values(validators);
const allValidatorKeysCount = validatorValues.length;
const requiredValidatorKeysCount =
allValidatorKeysCount - validatorValues.filter(isOptionalSchema).length;
const requiredValidatorKeysCount = validatorValues.reduce(
(acc, schema) => acc + (isOptionalSchema(schema) ? 0 : 1),
0,
);

if (
valueEntries.length > allValidatorKeysCount ||
Expand All @@ -91,14 +93,16 @@ export const validate = <S extends AnySchema>(schema: S) => (value: unknown): Ty
throw new ValidationError(schema, value);
}

return fromEntries(
valueEntries.map(([key, val]) => {
if (!validators[key]) {
throw new ValidationError(schema, value);
}
return [key, validate(validators[key]!)(val)];
}),
) as TypeOf<S>;
type Item = TypeOf<S>[keyof TypeOf<S>];
const obj = valueEntries.reduce((acc, [key, val]) => {
if (!validators[key]) {
throw new ValidationError(schema, value);
}
acc[key] = validate(validators[key]!)(val) as Item;
return acc;
}, {} as Record<string, Item>);

return obj as TypeOf<S>;
}

const parsedValue = parse(schema.__validator)(value);
Expand Down
4 changes: 2 additions & 2 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export const object = <U extends Record<string, AnySchema>>(obj: U) => {
return {
[TYPEOFWEB_SCHEMA]: true,
__validator: obj,
__type: {} as unknown,
__modifiers: { optional: false, nullable: false },
__type: {} as unknown,
__values: {} as unknown,
} as Schema<
{
Expand All @@ -159,8 +159,8 @@ export const array = <U extends readonly AnySchema[]>(...arr: readonly [...U]) =
return {
[TYPEOFWEB_SCHEMA]: true,
__validator: arr,
__type: {} as unknown,
__modifiers: { optional: false, nullable: false },
__type: {} as unknown,
__values: {} as unknown,
} as Schema<
readonly TypeOf<U[number]>[],
Expand Down

0 comments on commit c60a2a3

Please sign in to comment.