Socket
Socket
Sign inDemoInstall

fast-copy

Package Overview
Dependencies
0
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fast-copy

A blazing fast deep object copier


Version published
Maintainers
1
Install size
116 kB
Created

Package description

What is fast-copy?

The fast-copy npm package is a deep copying utility designed to be faster than other deep copy alternatives. It can handle various JavaScript data types and structures, including objects, arrays, dates, and more, providing a deep clone without the performance overhead of other libraries.

What are fast-copy's main functionalities?

Deep copying objects

This feature allows you to create a deep copy of an object, ensuring that nested objects are also cloned rather than just their references.

const copy = require('fast-copy').default;
const original = { a: 1, b: { c: 2 } };
const cloned = copy(original);

Deep copying arrays

Similar to objects, this feature enables deep copying of arrays, including nested arrays.

const copy = require('fast-copy').default;
const original = [1, 2, [3, 4]];
const cloned = copy(original);

Copying other types

fast-copy can also clone other JavaScript types such as Date objects and regular expressions.

const copy = require('fast-copy').default;
const date = new Date();
const regex = new RegExp('ab+c', 'i');
const clonedDate = copy(date);
const clonedRegex = copy(regex);

Other packages similar to fast-copy

Changelog

Source

2.1.7

  • Republish of 2.1.6, as the release process failed mid-publish

Readme

Source

fast-copy

A blazing fast deep object copier

Table of contents

Usage

import copy from "fast-copy";
import { deepEqual } from "fast-equals";

const object = {
  array: [123, { deep: "value" }],
  map: new Map([["foo", {}], [{ bar: "baz" }, "quz"]])
};

const copiedObject = copy(object);

console.log(copiedObject === object); // false
console.log(deepEqual(copiedObject, object)); // true

Options

isStrict

Starting in 2.0.0, you can use the isStrict option to copy the object based on strict standards, meaning:

  • Properties retain their original property descriptor
  • Non-enumerable properties are copied
  • Non-standard properties (e.g., keys on an Array object) are copied

This is significantly slower, so you should only use this if you believe it necessary.

console.log(copy(object, { isStrict: true }));

NOTE: This option is also aliased as copy.strict.

console.log(copy.strict(object));
realm

Under the hood, fast-copy uses instanceof to determine object types, which can cause false negatives when used in combination with iframe-based objects. To handle this edge case, you can pass the realm in options, which identifies which realm the object comes from and will use that realm to drive both comparisons and constructors for the copies.

<iframe srcdoc="<script>var arr = ['foo', 'bar'];</script>"></iframe>
const iframe = document.querySelector("iframe");
const arr = iframe.contentWindow.arr;

console.log(copy(arr, { realm: iframe.contentWindow })); // ['foo', 'bar']

Types supported

The following object types are deeply cloned when they are either properties on the object passed, or the object itself:

  • Array
  • ArrayBuffer
  • Blob
  • Buffer
  • DataView
  • Date
  • Float32Array
  • Float64Array
  • Int8Array
  • Int16Array
  • Int32Array
  • Map
  • Object
  • RegExp
  • Set
  • Uint8Array
  • Uint8ClampedArray
  • Uint16Array
  • Uint32Array
  • React components
  • Custom constructors

The following object types are copied directly, as they are either primitives, cannot be cloned, or the common use-case implementation does not expect cloning:

  • AsyncFunction
  • Boolean
  • Error
  • Function
  • GeneratorFunction
  • Number
  • Null
  • Promise
  • String
  • Symbol
  • Undefined
  • WeakMap
  • WeakSet

Circular objects are supported out of the box as well. By default a cache based on WeakSet is used, but if WeakSet is not available then a standard Object fallback is used. The benchmarks quoted below are based on use of WeakSet.

Benchmarks

Simple objects

Small number of properties, all values are primitives

Operations / second
fast-copy2,692,822
clone1,420,277
lodash.cloneDeep1,277,213
fast-deepclone768,982
ramda719,948
fast-clone567,342
deepclone509,547
fast-copy (strict)420,804
Complex objects

Large number of properties, values are a combination of primitives and complex objects

Operations / second
fast-copy109,352
fast-deepclone101,808
ramda93,103
deepclone74,270
fast-clone49,911
clone46,355
lodash.cloneDeep43,900
fast-copy (strict)33,440
Big data

Very large number of properties with high amount of nesting, mainly objects and arrays

Operations / second
fast-copy123
fast-deepclone101
fast-clone93
lodash.cloneDeep92
deepclone66
clone50
fast-copy (strict)42
ramda5
Circular objects

Objects that deeply reference themselves

Operations / second
fast-copy1,143,074
ramda750,430
clone722,632
lodash.cloneDeep580,005
deepclone490,824
fast-deepclone446,585
fast-copy (strict)321,678
fast-clone (not supported)0
Special objects

Custom constructors, React components, etc

Operations / second
fast-copy78,422
clone52,165
lodash.cloneDeep39,648
ramda32,372
fast-deepclone27,518
fast-clone27,495
deepclone16,552
fast-copy (strict)12,509

Development

Standard practice, clone the repo and yarn (or npm i) to get the dependencies. The following npm scripts are available:

  • benchmark => run benchmark tests against other equality libraries
  • build => build dist files with rollup
  • clean => run rimraf on the dist folder
  • dev => start webpack playground App
  • dist => run build and build:minified scripts
  • lint => run ESLint on all files in src folder (also runs on dev script)
  • lint:fix => run lint script, but with auto-fixer
  • prepublishOnly => run lint, test:coverage, and dist scripts
  • release => run prepublishOnly and release with new version
  • release:beta => run prepublishOnly and release with new beta version
  • release:dry => run prepublishOnly and simulate a new release
  • start => run dev
  • test => run AVA with NODE_ENV=test on all files in test folder
  • test:coverage => run same script as test with code coverage calculation via nyc
  • test:watch => run same script as test but keep persistent watcher

Keywords

FAQs

Last updated on 23 Sep 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc