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 structuredClone for deep copying #1105

Merged
merged 3 commits into from
Oct 12, 2023

Conversation

JKRhb
Copy link
Member

@JKRhb JKRhb commented Oct 2, 2023

JavaScript nowadays has a built-in function for deep-cloning called structuredClone which makes it possible to get rid of the JSON.parse(JSON.stringify(foo)) construction. I found a few places where the new function can be used, hopefully increasing readability and performance a bit.

Cases where the parse-stringify-approach still needs to be used could be revisited in the future to arrive at a slightly cleaner implementation.

@JKRhb JKRhb changed the title refactor: use structuredClone for deep coyping refactor: use structuredClone for deep copying Oct 2, 2023
@codecov
Copy link

codecov bot commented Oct 2, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (49ad831) 75.52% compared to head (33af731) 75.54%.
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1105      +/-   ##
==========================================
+ Coverage   75.52%   75.54%   +0.01%     
==========================================
  Files          80       80              
  Lines       16153    16162       +9     
  Branches     1520     1513       -7     
==========================================
+ Hits        12200    12209       +9     
  Misses       3914     3914              
  Partials       39       39              
Files Coverage Δ
packages/binding-http/src/http-server.ts 83.25% <100.00%> (ø)
packages/core/src/consumed-thing.ts 83.35% <100.00%> (-0.03%) ⬇️
packages/core/src/exposed-thing.ts 67.63% <100.00%> (+0.47%) ⬆️
packages/core/src/helpers.ts 83.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -220,7 +220,7 @@ export default class Helpers implements Resolver {
* Helper function to remove reserved keywords in required property of TD JSON Schema
*/
static createExposeThingInitSchema(tdSchema: unknown): SomeJSONSchema {
const tdSchemaCopy = JSON.parse(JSON.stringify(tdSchema));
const tdSchemaCopy = structuredClone(tdSchema) as SomeJSONSchema;
Copy link
Member Author

Choose a reason for hiding this comment

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

I added this as another instance suitable for using the structuredClone function – the typing is not that nice here, but I think casting should be fine here since we are performing type checks down below.

Copy link
Member

Choose a reason for hiding this comment

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

I guess it is acceptable, we could even put it as the argument type given the fact that is just an internal helper function

Copy link
Member

@relu91 relu91 left a comment

Choose a reason for hiding this comment

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

Good to go!

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.

None yet

3 participants