-
Notifications
You must be signed in to change notification settings - Fork 80
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
Conversation
structuredClone
for deep coypingstructuredClone
for deep copying
Codecov ReportAll modified lines are covered by tests ✅
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
☔ View full report in Codecov by Sentry. |
@@ -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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to go!
JavaScript nowadays has a built-in function for deep-cloning called
structuredClone
which makes it possible to get rid of theJSON.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.