Skip to content

Instantly share code, notes, and snippets.

@receptor
Last active February 2, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save receptor/ecc4742ecd931ec290bba3b7887ad7ea to your computer and use it in GitHub Desktop.
Save receptor/ecc4742ecd931ec290bba3b7887ad7ea to your computer and use it in GitHub Desktop.
javascript clone
// usage
// const obj = /* ... */;
// const sc = await jsclone.structuralClone(obj);
// const c = jsclone.clone(obj);
moudule.exports = {
structuralClone(obj) => {
return new Promise(resolve => {
const {port1, port2} = new MessageChannel();
port2.onmessage = ev => resolve(ev.data);
port1.postMessage(obj);
});
}
clone(obj) => JSON.parse(JSON.stringify(obj));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment