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

Support for structuredClone #684

Closed
Andarius opened this issue Mar 1, 2022 · 8 comments
Closed

Support for structuredClone #684

Andarius opened this issue Mar 1, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@Andarius
Copy link

Andarius commented Mar 1, 2022

Problem

To deep clone an object today, one can either use JSON.stringify or lodash.cloneDeep. There is no dedicated way of creating a deep copy in an efficient manner.

Solution

A solution could be implementing: structuredClone (see https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)

@Andarius Andarius added the enhancement New feature or request label Mar 1, 2022
@ljharb
Copy link

ljharb commented Mar 1, 2022

structuredClone isn’t part of the language; but it may be in the medium term. Currently, it’s a web api, so I’m not sure it makes sense for Hermes to have it.

@Andarius Andarius closed this as completed Mar 1, 2022
@tmikov
Copy link
Contributor

tmikov commented Mar 1, 2022

I think one way to look at this is, is it provided by the engine or the browser? For example, is it available in Node?

@ljharb
Copy link

ljharb commented Mar 1, 2022

node does not provide it currently. If they did, it'd be like setTimeout, where they're mirroring the browser without a spec compelling them to do so.

@migueldaipre
Copy link

Hey guys, In node v17 it is already present, doesn't it make sense to have it in Hermes too?
https://nodejs.org/en/blog/release/v17.0.0/

@joe-sam
Copy link

joe-sam commented Feb 25, 2023

structuredClone is now supported in the facebook/jest and facebook/lexical repository.
Right now the only way is 3d party libs which often don't work correctly or the spread operator which only does a shallow copy.

@cjshearer
Copy link

I'd love to see this in react-native. structuredClone is available in the majority of other environments and provides significant performance advantages over js implementations that don't have direct memory access.

@tmikov
Copy link
Contributor

tmikov commented Jun 30, 2023

Note that in each case structuredClone() doesn't seem to be provided by the engine itself, instead it is provided by the integration (the browser, etc). At least my experiments with all the engines available for mac64arm from jsvu show that it isn't available in any.

People commonly confuse the browser with the engine itself. This has been a common topic in several issues submitted here, where users are requesting APIs that are not part of the EcmaScript spec. I have suggested that it might make sense to create community "integration" repository for such extensions, but there has been no interest.

With all that said, admittedly, an implementation of structuredClone() based on JSI would likely be significantly slower than one using internal Hermes APIs. So, theoretically there would be some value in a built-in implementation. The problem is that builtin APIs change all the time and increasing the usage area increases the maintenance cost.

Since Hermes will have a stable release schedule starting with the next release, it might make sense to allow external users of internal APIs, and a way to package and distribute them conveniently. In this way it would be possible to maintain efficient Hermes-specific extensions, with some cost per-release, without making them part of Hermes itself.

@tmikov
Copy link
Contributor

tmikov commented Jul 28, 2023

FYI #1072

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants