Upgrade structuredClone
options object with skipNotSerializable
to avoid cloning exceptions
#10255
Labels
addition/proposal
New features or enhancements
needs implementer interest
Moving the issue forward requires implementers to express interest
topic: serialize and transfer
What problem are you trying to solve?
Practically, I want to replace
JSON.parse(JSON.stringify(obj))
withstructuredClone(obj)
, but there is a big difference in behavior - the exception thestructuredClone
throws when it finds anything that can't be serialized, for example afunction
.Example:
What solutions exist today?
Catching the exception doesn't help at all, because you still need a clone, so you need to clean the object first, which is likely much more work than the original
JSON
solution.How would you solve it?
By providing a new switch, for example
skipNotSerializable
.This would cause cloning algorithm to ignore properties that can't be cloned while still returning a clone of the rest of the object.
Anything else?
MDN:
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone#options
Review of structured clone algorithms:
tc39/ecma262#2555
The text was updated successfully, but these errors were encountered: