Skip to content

Commit

Permalink
Merge pull request #10 from takagiy/feat/avoid-set-prototype-of
Browse files Browse the repository at this point in the history
refactor: use `Object.create` instead of `Object.setPrototypeOf` to improve performance
  • Loading branch information
takagiy authored Aug 27, 2024
2 parents 318600e + 56d407d commit 7ca28fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
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

0 comments on commit 7ca28fd

Please sign in to comment.