Last active
February 2, 2018 15:57
-
-
Save receptor/ecc4742ecd931ec290bba3b7887ad7ea to your computer and use it in GitHub Desktop.
javascript clone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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