• Stars
    star
    1,133
  • Rank 41,104 (Top 0.9 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A blazing fast deep object copier

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

API

copy

Deeply copy the object passed.

import copy from 'fast-copy';

const copied = copy({ foo: 'bar' });

copyStrict

Deeply copy the object passed, but with additional strictness when replicating the original object:

  • Properties retain their original property descriptor
  • Non-enumerable keys are copied
  • Non-standard properties (e.g., keys on arrays / maps / sets) are copied
import { copyStrict } from 'fast-copy';

const object = { foo: 'bar' };
object.nonEnumerable = Object.defineProperty(object, 'bar', {
  enumerable: false,
  value: 'baz',
});

const copied = copy(object);

NOTE: This method is significantly slower than copy, so it is recommended to only use this when you have specific use-cases that require it.

createCopier

Create a custom copier based on the type-specific methods passed. This is useful if you want to squeeze out maximum performance, or perform something other than a standard deep copy.

import { createCopier } from 'fast-copy';

const copyShallow = createCopier({
  array: (array) => [...array],
  map: (map) => new Map(map.entries()),
  object: (object) => ({ ...object }),
  set: (set) => new Set(set.values()),
});

Each internal copier method has the following contract:

type InternalCopier<Value> = (value: Value, state: State) => Value;

interface State {
  Constructor: any;
  cache: WeakMap;
  copier: InternalCopier<any>;
  prototype: any;
}

Any method overriding the defaults must maintain this contract.

Copier methods

  • array => Array
  • arrayBuffer=> ArrayBuffer, Float32Array, Float64Array, Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, Uint64Array
  • blob => Blob
  • dataView => DataView
  • date => Date
  • error => Error, AggregateError, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError
  • map => Map
  • object => Object, or any custom constructor
  • regExp => RegExp
  • set => Set

Copier state

cache

If you want to maintain circular reference handling, then you'll need the methods to handle cache population for future lookups:

function shallowlyCloneArray<Value extends any[]>(
  value: Value,
  state: State
): Value {
  const clone = [...value];

  state.cache.set(value, clone);

  return clone;
}
copier

copier is provided for recursive calls with deeply-nested objects.

function deeplyCloneArray<Value extends any[]>(
  value: Value,
  state: State
): Value {
  const clone = [];

  state.cache.set(value, clone);

  value.forEach((item) => state.copier(item, state));

  return clone;
}

Note above I am using forEach instead of a simple map. This is because it is highly recommended to store the clone in cache eagerly when deeply copying, so that nested circular references are handled correctly.

Constructor / prototype

Both Constructor and prototype properties are only populated with complex objects that are not standard objects or arrays. This is mainly useful for custom subclasses of these globals, or maintaining custom prototypes of objects.

function deeplyCloneSubclassArray<Value extends CustomArray>(
  value: Value,
  state: State
): Value {
  const clone = new state.Constructor();

  state.cache.set(value, clone);

  value.forEach((item) => clone.push(item));

  return clone;
}

function deeplyCloneCustomObject<Value extends CustomObject>(
  value: Value,
  state: State
): Value {
  const clone = Object.create(state.prototype);

  state.cache.set(value, clone);

  Object.entries(value).forEach(([k, v]) => (clone[k] = v));

  return clone;
}

createStrictCopier

Create a custom copier based on the type-specific methods passed, but defaulting to the same functions normally used for copyStrict. This is useful if you want to squeeze out better performance while maintaining strict requirements, or perform something other than a strict deep copy.

const createStrictClone = (value, clone) =>
  Object.getOwnPropertyNames(value).reduce(
    (clone, property) =>
      Object.defineProperty(
        clone,
        property,
        Object.getOwnPropertyDescriptor(value, property) || {
          configurable: true,
          enumerable: true,
          value: clone[property],
          writable: true,
        }
      ),
    clone
  );

const copyStrictShallow = createStrictCopier({
  array: (array) => createStrictClone(array, []),
  map: (map) => createStrictClone(map, new Map(map.entries())),
  object: (object) => createStrictClone(object, {}),
  set: (set) => createStrictClone(set, new Set(set.values())),
});

NOTE: This method creates a copier that is significantly slower than copy, as well as likely a copier created by createCopier, so it is recommended to only use this when you have specific use-cases that require it.

Types supported

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

  • Array
  • ArrayBuffer
  • Boolean primitive wrappers (e.g., new Boolean(true))
  • Blob
  • Buffer
  • DataView
  • Date
  • Float32Array
  • Float64Array
  • Int8Array
  • Int16Array
  • Int32Array
  • Map
  • Number primitive wrappers (e.g., new Number(123))
  • Object
  • RegExp
  • Set
  • String primitive wrappers (e.g., new String('foo'))
  • 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 primitives
  • Error
  • Function
  • GeneratorFunction
  • Number primitives
  • Null
  • Promise
  • String primitives
  • Symbol
  • Undefined
  • WeakMap
  • WeakSet

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

Aspects of default copiers

Inherently, what is considered a valid copy is subjective because of different requirements and use-cases. For this library, some decisions were explicitly made for the default copiers of specific object types, and those decisions are detailed below. If your use-cases require different handling, you can always create your own custom copier with createCopier or createStrictCopier.

Error references are copied directly, instead of creating a new *Error object

While it would be relatively trivial to copy over the message and stack to a new object of the same Error subclass, it is a common practice to "override" the message or stack, and copies would not retain this mutation. As such, the original reference is copied.

The constructor of the original object is used, instead of using known globals

Starting in ES2015, native globals can be subclassed like any custom class. When copying, we explicitly reuse the constructor of the original object. However, the expectation is that these subclasses would have the same constructur signature as their native base class. This is a common community practice, but there is the possibility of inaccuracy if the contract differs.

Generator objects are copied, but still reference the original generator's state

Generator objects are specific types of iterators, but appear like standard objects that just have a few methods (next, throw, return). These methods are bound to the internal state of the generator, which cannot be copied effectively. Normally this would be treated like other "uncopiable" objects and simply pass the reference through, however the "validation" of whether it is a generator object or a standard object is not guaranteed (duck-typing) and there is a runtime cost associated with. Therefore, the simplest path of treating it like a standard object (copying methods to a new object) was taken.

Benchmarks

Simple objects

Small number of properties, all values are primitives

Operations / second
fast-copy 5,880,312
lodash.cloneDeep 2,706,261
clone 2,207,231
deepclone 1,274,810
fast-clone 1,239,952
ramda 1,146,152
fast-copy (strict) 852,382

Complex objects

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

Operations / second
fast-copy 162,858
ramda 142,104
deepclone 133,607
fast-clone 101,143
clone 70,872
fast-copy (strict) 62,961
lodash.cloneDeep 62,060

Big data

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

Operations / second
fast-copy 303
fast-clone 245
deepclone 151
lodash.cloneDeep 150
clone 93
fast-copy (strict) 90
ramda 42

Circular objects

Objects that deeply reference themselves

Operations / second
fast-copy 2,420,466
deepclone 1,386,896
ramda 1,024,108
lodash.cloneDeep 989,796
clone 987,721
fast-copy (strict) 617,602
fast-clone 0 (not supported)

Special objects

Custom constructors, React components, etc

Operations / second
fast-copy 152,792
clone 74,347
fast-clone 66,576
lodash.cloneDeep 64,760
ramda 53,542
deepclone 28,823
fast-copy (strict) 21,362

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 => run build:esm, build:cjs, build:umd, and build:min scripts
  • build:cjs => build CJS files and types
  • build:esm => build ESM files and types
  • build:min => build minified files and types
  • build:umd => build UMD files and types
  • clean => run rimraf on the dist folder
  • dev => start webpack playground App
  • dist => run clean and build 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
  • typecheck => run tsc on the codebase

More Repositories

1

moize

The consistently-fast, complete memoization solution for JS
TypeScript
892
star
2

fast-equals

A blazing fast equality comparison, either shallow or deep
TypeScript
471
star
3

unchanged

A tiny, fast, unopinionated handler for updating JS objects and arrays immutably
TypeScript
240
star
4

micro-memoize

A tiny, crazy fast memoization library for the 95% use-case
TypeScript
238
star
5

crio

Immutable objects and arrays in a natural way
JavaScript
211
star
6

hash-it

Hash any object type based on its values
TypeScript
206
star
7

remeasure

Get position and size attributes for any React Component
JavaScript
140
star
8

inline-loops.macro

Iteration helpers that inline to native loops for performance
JavaScript
100
star
9

selectorator

Simple generator of reselect selectors
TypeScript
96
star
10

react-style-tag

Write styles declaratively in React
TypeScript
68
star
11

react-pure-lifecycle

JavaScript
65
star
12

react-windowed-list

JavaScript
61
star
13

jile

Modular CSS in pure JavaScript
JavaScript
59
star
14

react-vidz-player

HTML5 videos in a React way
JavaScript
53
star
15

fast-stringify

A blazing fast stringifier that safely handles circular objects
TypeScript
51
star
16

react-billboardjs

React component for the billboard.js charting library
JavaScript
45
star
17

curriable

Curry any function with placeholder support
TypeScript
38
star
18

vidz

A zero-dependency, framework-agnostic video implementation
JavaScript
37
star
19

switchem

An extensible, functional switch with a chainable API
JavaScript
35
star
20

waddup

A ridiculously tiny pubsub manager with no dependencies
JavaScript
27
star
21

benchee

Simple benchmarks in both node and browser
TypeScript
27
star
22

react-local-redux

Manage component-specific state as you would global state via redux
JavaScript
23
star
23

arco

JavaScript
22
star
24

flexor

JavaScript
21
star
25

qonductor

Manage your data processing with sanity
JavaScript
19
star
26

react-parm

Handle react class instances with more functional purity
JavaScript
18
star
27

react-rendered-size

Get the rendered size of a React element without needing to render it
JavaScript
17
star
28

kari

JavaScript
14
star
29

pathington

JavaScript
13
star
30

get-object-class

A more explicit improvement on typeof
JavaScript
12
star
31

redux-browser-storage

Use redux to manage localStorage and sessionStorage data
JavaScript
11
star
32

remodeled

An abstraction for the React API with functional purity
JavaScript
11
star
33

convertify

Easily convert from one object class to the next
JavaScript
10
star
34

nage

Efficient, tiny object pool
TypeScript
9
star
35

tcf

A functional try / catch / finally with async support
JavaScript
8
star
36

bolster-css

JavaScript
7
star
37

printscout

Handle print events with ease
JavaScript
7
star
38

pure-object

JavaScript
7
star
39

react-jile

JavaScript
7
star
40

highcharts-config

Declarative Highcharts configuration generator with immutable, chainable API
JavaScript
6
star
41

react-idle-manager

JavaScript
6
star
42

react-redux-partitioner

Distribute state management for more performant reactivity
TypeScript
5
star
43

repoll

JavaScript
5
star
44

isit.js

Micro check library
JavaScript
4
star
45

identitate

Custom identity functions for composability
JavaScript
4
star
46

retip

A simple react tooltip
JavaScript
3
star
47

redux-slices

Manage slices of redux store in a concise, clear way
HTML
2
star
48

utilities

A collection of utilities used across projects
JavaScript
2
star
49

doozy

Transducer library for arrays, objects, sets, and maps
JavaScript
2
star
50

what-am-i

Simple validation library
TypeScript
2
star
51

promise-polyfill

Promise polyfill with custom opt-in error handling for debug
TypeScript
1
star
52

bolster

Library to augment jQuery with additional functionality
JavaScript
1
star
53

memzee

Function memoization based on only the most recent arguments
TypeScript
1
star
54

isifier

Make your own tiny, targeted validation library
JavaScript
1
star
55

diviso

Simple, flexible state management
1
star
56

singulum

State management with sanity
JavaScript
1
star
57

planttheidea.github.io

Github IO site for planttheidea
JavaScript
1
star