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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@chaisser/deep-clone

v1.0.1

Published

Fast deep clone for objects and arrays

Readme

📋 @chaisser/deep-clone

Fast deep and shallow clone for objects, arrays, Dates, RegExps, Maps, and Sets


✨ Features

  • 🎯 Type-safe - Full TypeScript support with generics
  • 🔄 Deep clone - Recursively copies nested objects, arrays, Maps, Sets, Dates, RegExps
  • 📄 Shallow clone - One-level copy when you don't need recursion
  • 🗺️ Map & Set - Full support for Map and Set cloning
  • 📅 Date & RegExp - Properly clones Date and RegExp instances
  • 🛡️ Immutable - Never mutates the original value
  • 🪶 Zero dependencies - Lightweight and tree-shakeable
  • 🏎️ ESM + CJS - Dual module format support

📦 Installation

npm install @chaisser/deep-clone
# or
yarn add @chaisser/deep-clone
# or
pnpm add @chaisser/deep-clone

🚀 Quick Start

import { deepClone, shallowClone } from '@chaisser/deep-clone';

// Deep clone
const original = { user: { name: 'Doruk', hobbies: ['coding'] } };
const cloned = deepClone(original);

cloned.user.name = 'Alice';
cloned.user.hobbies.push('music');

console.log(original.user.name);    // 'Doruk' — unchanged
console.log(original.user.hobbies); // ['coding'] — unchanged

// Shallow clone
const obj = { a: 1, b: { c: 2 } };
const shallow = shallowClone(obj);

shallow.b.c = 99;
console.log(obj.b.c); // 99 — nested objects are shared references

📖 What It Does

This package provides deep and shallow cloning utilities for JavaScript values. deepClone recursively copies all nested structures — objects, arrays, Maps, Sets, Dates, and RegExps — producing a fully independent clone. shallowClone copies only the top level, keeping nested references shared with the original.


🎯 How It Works

The package provides 2 functions:

  • deepClone - Recursively clones any value including nested objects, arrays, Maps, Sets, Dates, and RegExps
  • shallowClone - Copies one level deep using spread ({...obj}) or .slice() for arrays

deepClone handles these types:

  • Primitives (null, undefined, string, number, boolean, symbol, bigint)
  • Plain objects
  • Arrays
  • Date instances
  • RegExp instances
  • Map instances
  • Set instances

🎨 What It's Useful For

  • Immutable State - Clone before mutating (Redux, state management)
  • Data Isolation - Prevent shared reference bugs
  • Undo/Redo - Snapshot state at a point in time
  • Testing - Clone fixtures to avoid test pollution
  • Configuration - Deep copy defaults before overriding
  • Caching - Return clones from cache to prevent external mutation

💡 Usage Examples

Objects

import { deepClone } from '@chaisser/deep-clone';

const original = {
  name: 'Doruk',
  address: {
    city: 'Istanbul',
    coords: { lat: 41.01, lng: 28.98 },
  },
};

const clone = deepClone(original);
clone.address.city = 'Ankara';

console.log(original.address.city); // 'Istanbul' — unaffected

Arrays

const original = [[1, 2], [3, 4], { key: 'value' }];
const clone = deepClone(original);

clone[0].push(3);
clone[2].key = 'changed';

console.log(original[0]);    // [1, 2] — unaffected
console.log(original[2].key); // 'value' — unaffected

Maps and Sets

const original = new Map([
  ['user', { name: 'Doruk' }],
  ['scores', new Set([10, 20, 30])],
]);

const clone = deepClone(original);
clone.get('user')!.name = 'Alice';
(clone.get('scores') as Set<number>).add(40);

console.log(original.get('user')!.name); // 'Doruk' — unaffected
console.log((original.get('scores') as Set<number>).has(40)); // false

Dates and RegExps

const original = {
  date: new Date('2024-01-01'),
  pattern: /^hello\s+world$/gi,
};

const clone = deepClone(original);
clone.date.setFullYear(2025);

console.log(original.date.getFullYear()); // 2024 — unaffected
console.log(clone.pattern.source);         // '^hello\\s+world$'
console.log(clone.pattern.flags);          // 'gi'

Primitives

deepClone(42);          // 42
deepClone('hello');     // 'hello'
deepClone(null);        // null
deepClone(undefined);   // undefined
deepClone(true);        // true

Shallow Clone

import { shallowClone } from '@chaisser/deep-clone';

const original = { a: 1, nested: { b: 2 } };
const clone = shallowClone(original);

clone.a = 99;
clone.nested.b = 99;

console.log(original.a);       // 1 — top level is independent
console.log(original.nested.b); // 99 — nested is shared!

📚 API Reference

deepClone<T>(value: T): T

Recursively clone any value.

| Parameter | Type | Description | |---|---|---| | value | T | Any value to clone |

Returns: A deep, independent copy of value

Supported types:

  • Primitives returned as-is (null, undefined, string, number, boolean, symbol, bigint)
  • Plain objects — cloned key by key
  • Arrays — mapped element by element
  • Date — new Date with same timestamp
  • RegExp — new RegExp with same source and flags
  • Map — cloned with recursively cloned keys and values
  • Set — cloned with recursively cloned values

shallowClone<T>(value: T): T

Clone one level deep. Nested objects/arrays are shared references.

| Parameter | Type | Description | |---|---|---| | value | T | Any value to clone |

Returns: A shallow copy of value


🔗 Related Packages

Explore our other utility packages in the @chaisser namespace:


🔒 License

MIT - Free to use in personal and commercial projects


👨 Developed by

Doruk Karaboncuk [email protected]


📄 Repository


🤝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

📞 Support

For issues, questions, or suggestions, please reach out through:


Made with ❤️ by @chaisser

npm license downloads typescript