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

fix(cloneDeep): maintain prototype when cloning class instances #794

Merged
merged 8 commits into from
Nov 10, 2024

Conversation

filipsobol
Copy link
Contributor

@filipsobol filipsobol commented Nov 5, 2024

Addresses #777 and #801.

Benchmark results

Benchmark results

Copy link

vercel bot commented Nov 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
es-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 9, 2024 6:15pm

@codecov-commenter
Copy link

codecov-commenter commented Nov 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.08%. Comparing base (0feabb4) to head (0745767).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #794      +/-   ##
==========================================
- Coverage   99.11%   99.08%   -0.04%     
==========================================
  Files         305      305              
  Lines        2727     2727              
  Branches      800      800              
==========================================
- Hits         2703     2702       -1     
- Misses         23       24       +1     
  Partials        1        1              

@@ -184,7 +185,9 @@ function cloneDeepImpl<T>(obj: T, stack = new Map<any, any>()): T {
}

if (typeof obj === 'object' && obj !== null) {
const result = {};
const result =
typeof obj.constructor === 'function' && !isPrototype(obj) ? Object.create(Object.getPrototypeOf(obj)) : {};
Copy link
Collaborator

@raon0211 raon0211 Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m curious why we check if obj.constructor is a function and if obj is not a prototype here. Wouldn’t just Object.create(Object.getPrototypeOf(obj)) be sufficient?

Copy link
Contributor Author

@filipsobol filipsobol Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that calling Object.create() and Object.getPrototypeOf() on every object would be expensive and hurt performance, so I decided to add a check to ensure that we only call it on classes. But it turned out to be the opposite! Using just the Object.create(Object.getPrototypeOf(obj)) improved performance by about 15%. I updated the PR.

@@ -238,4 +238,28 @@ describe('clone', () => {
expect(clonedError.message).toBe(error.message);
expect(clonedError.name).toBe(error.name);
});

it('should clone class instance', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test code is added in clone, not cloneDeep. Let me add a test case for cloneDeep, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There already is a should clone instance test in cloneDeep.spec.ts, so that's why I only added a single test covering this case.

Copy link
Collaborator

@raon0211 raon0211 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@raon0211 raon0211 merged commit 894a645 into toss:main Nov 10, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants