npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

realistic-structured-clone

v3.0.0

Published

A pure JS implementation of the structured clone algorithm (or at least something pretty close to that)

Downloads

1,321,995

Readme

Realistic Structured Clone Build Status

You might not need this anymore! A native structuredClone function is available in many recent environments, such as Node v17 and Firefox v94. Check if your target environment has structuredClone built in. If it does, use that.

This is a pure JS implementation of the structured clone algorithm (or at least something pretty close to that).

Why do you want this? Well, you probably don't. If your goal is to just clone a JS object, you're better off with lodash's _.cloneDeep or the popular clone module on npm.

Let's try again... why do you want this? If you are making an implementation of an API that explicitly uses the structured clone algorithm (such as IndexedDB), then you want something that handles quirks and edge cases exactly like the structured clone algorithm. That's what realistic-structured-clone is for. It's not totally there (see below) but it's a decent start.

Use

Install through npm:

$ npm install realistic-structured-clone

Then use it:

// First load the module
// (Use Browserify or something if you're targeting the web)
var structuredClone = require('realistic-structured-clone');

// Clone a variable (will throw a DataCloneError for invalid input)
var clonedX = structuredClone(x);

Alternatives

If you look around, you'll notice various modules calling themselves implementations of the structured clone algorithm, such as the structured-clone package on npm. But that package, like all the others I've seen, doesn't actually seem to be an attempt at implementing the structured clone algorithm. It's just some arbitrary type of clone. As I wrote above, this distinction only matters if you really care about the nuances of the structured clone algorithm, which you probably don't.

If you're working in the browser, you can do something like this to do a real structured clone:

function clone(x) {
    return new Promise(function (resolve, reject) {
        window.addEventListener('message', function(e) {
            resolve(e.data);
        });
        window.postMessage(x, "*");
    });
}
var x = {a:[1,2,3], b:{c:1}};
clone(x).then(function(cloned) {
    console.log("x: %s", JSON.stringify(x));
    console.log("cloned: %s", JSON.stringify(cloned));
    console.log("x == cloned %s", x == cloned);
    console.log("x === cloned %s", x === cloned);
});

However, that won't help you in Node.js. It's also asynchronous, which could be a problem. realistic-structured-clone is synchronous and works everywhere.

Current State

As of version 2.0, it should be pretty damn close to the spec! However it is now just a light wrapper around the Typeson structured-cloning-throwing preset.

License

Apache 2.0