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

fast-deepclone

v1.0.1

Published

Deep clone an object

Downloads

65

Readme

Build Status

fast-deepclone

Fast and safe JSON deep-cloning utility

Behavior

POJOs and arrays are always duplicated.

Circular references are carried over to the cloned object.

Objects properties and prototypes are ignored.

The resulting duplicated objects keep the same references to the following objects:

  • functions and generator functions
  • native Error objects
  • WeakMap
  • WeakSet
  • Promise
  • symbols
  • object version of scalar values (e.g. BooleanObject)

This module also allows to either keep references or to duplicate the following objects, depending on the provided options:

  • Buffer
  • TypedArrays
  • Map
  • Set
  • Date
  • RegExp

Installation

npm fast-deepclone

This module can only be used with NodeJS (tested on all major versions from v4 to v8 included).

How to use

const deepClone = require('fast-deepclone');


/*
  Clone objects, keeping references to Buffer, TypedArrays,
  Map, Set, Date and RegExp.
  This means that modifying one of those objects in the
  source object will also impact the target object
 */
const clonedTarget = deepClone(source);
// or: const clonedTarget = deepClone(source, false);

/*
  Copy objects: new Buffer, TypedArrays, Map, Set, Date
  and RegExp objects are created in the target object,
  and their values are copied from the source
 */
const copiedTarget = deepClone(source, true);

When to use this module

This module is written in C++, using V8 for faster object access.
This is, to my knowledge, the fastest deep-cloning module currently available, but it can only work with NodeJS.

If you need a deep-cloning tool in browsers, check either lodash.cloneDeep or clone. These modules are well-written, well-tested, and they also handle circular references/JS Objects.

Moreover, if all you need to duplicate are POJO objects (i.e. objects containing only objects, arrays or scalar values), and if you are absolutely certain that there is no circular reference in them, then you may want to use JSON.parse(JSON.stringify(<object_to_clone>)) instead.
Duplicating objects using JSON functions is way faster. Around 2x to 6x faster than this module, depending on the nature of the duplicated object.
This is because these functions are hardcoded within V8 and have access to internal structures, not exposed in the V8 public API. Which is not necessarily a bad thing, if a bit frustrating.

Benchmarks

Obtained by running node benchmark.js at the root of the project, using NodeJS v10.9.0:

== Empty objects
lodash.cloneDeep x 2,518,033 ops/sec ±1.44% (92 runs sampled)
clone x 2,836,807 ops/sec ±2.49% (89 runs sampled)
fast-deepclone (clone mode) x 3,701,173 ops/sec ±0.70% (94 runs sampled)
fast-deepclone (copy mode) x 3,517,442 ops/sec ±0.64% (90 runs sampled)

== Fast POJO objects (5 properties, depth === 3)
lodash.cloneDeep x 206,573 ops/sec ±0.66% (98 runs sampled)
clone x 249,522 ops/sec ±1.45% (92 runs sampled)
fast-deepclone (clone mode) x 133,702 ops/sec ±0.68% (94 runs sampled)
fast-deepclone (copy mode) x 129,638 ops/sec ±0.59% (95 runs sampled)

== Slow POJO objects (100 properties, depth === 100)
lodash.cloneDeep x 254 ops/sec ±1.19% (84 runs sampled)
clone x 297 ops/sec ±1.51% (87 runs sampled)
fast-deepclone (clone mode) x 519 ops/sec ±0.51% (91 runs sampled)
fast-deepclone (copy mode) x 510 ops/sec ±0.73% (90 runs sampled)

== Fast objects w/ constructors and circular refs
lodash.cloneDeep x 15,130 ops/sec ±1.02% (91 runs sampled)
clone x 15,333 ops/sec ±19.09% (76 runs sampled)
fast-deepclone (clone mode) x 87,303 ops/sec ±0.24% (95 runs sampled)
fast-deepclone (copy mode) x 45,266 ops/sec ±0.88% (91 runs sampled)

License

MIT