Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use Object.create instead of Object.setPrototypeOf to improve performance #10

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "validated-extendable",
"version": "7.2.11",
"version": "7.2.12",
"description": "A library that allows you to define classes extending zod schemas to avoid boilerplate code.",
"exports": {
".": {
Expand Down
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ export const Validated = <
const validatedValue = schema.parse(value);
const wrapValue = !isObject(validatedValue) || options?.wrapValue;
const _this = wrapValue ? { value: validatedValue } : validatedValue;
Object.setPrototypeOf(_this, this);
return _this;
return Object.create(this, Object.getOwnPropertyDescriptors(_this));
} as unknown as ValidatedConstructor<
Schema,
Options extends { wrapValue: true } ? true : IsPrimitive<z.infer<Schema>>
Expand Down Expand Up @@ -88,8 +87,9 @@ export const ValidatedMutable = <
const validatedValueProxy = isObject(validatedValue)
? makeValidatedValueProxy(value)(validatedValue)
: validatedValue;
const _this = new Proxy(
{ value: validatedValueProxy },
const _this = { value: validatedValueProxy };
return new Proxy(
Object.create(this, Object.getOwnPropertyDescriptors(_this)),
{
set(object, propertyName, newValue) {
if (propertyName !== "value") {
Expand All @@ -103,12 +103,11 @@ export const ValidatedMutable = <
},
},
);
Object.setPrototypeOf(_this, this);
return _this;
}
const _this = makeValidatedValueProxy(value)(validatedValue);
Object.setPrototypeOf(_this, this);
return _this;
const _this = validatedValue;
return makeValidatedValueProxy(value)(
Object.create(this, Object.getOwnPropertyDescriptors(_this)),
);
} as unknown as ValidatedMutableConstructor<
Schema,
Options extends { wrapValue: true } ? true : IsPrimitive<z.infer<Schema>>
Expand Down