$ npm install fast-copy
A blazing fast deep object copier
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
copyDeeply copy the object passed.
import copy from 'fast-copy';
const copied = copy({ foo: 'bar' });
copyStrictDeeply copy the object passed, but with additional strictness when replicating the original object:
Array object) are copiedimport { 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.
createCopierCreate 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()),
});
createStrictCopierCreate 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 is significantly slower than copy, so it is recommended to only use this when you have specific use-cases that require it.
array => ArrayarrayBuffer=> ArrayBuffer, Float32Array, Float64Array, Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, Uint64Arrayblob => BlobdataView => DataViewdate => Dateerror => Error, AggregateError, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIErrormap => Mapobject => Object, or any custom constructorregExp => RegExpset => SetThe following object types are deeply cloned when they are either properties on the object passed, or the object itself:
ArrayArrayBufferBlobBufferDataViewDateFloat32ArrayFloat64ArrayInt8ArrayInt16ArrayInt32ArrayMapObjectRegExpSetUint8ArrayUint8ClampedArrayUint16ArrayUint32ArrayReact componentsThe following object types are copied directly, as they are either primitives, cannot be cloned, or the common use-case implementation does not expect cloning:
AsyncFunctionBooleanErrorFunctionGeneratorFunctionNumberNullPromiseStringSymbolUndefinedWeakMapWeakSetCircular 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.
Inherently, what is considered a valid copy is subjective because of different requirements and use-cases. For this library, some decisions were explicitly made.
*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.
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, however because there is the possibility of inaccuracy if the contract differs, it should be noted.
Small number of properties, all values are primitives
| Operations / second | |
|---|---|
| fast-copy | 5,743,509 |
| lodash.cloneDeep | 2,614,035 |
| clone | 2,173,098 |
| fast-clone | 1,341,313 |
| deepclone | 1,238,167 |
| ramda | 1,115,519 |
| fast-copy (strict) | 845,093 |
Large number of properties, values are a combination of primitives and complex objects
| Operations / second | |
|---|---|
| fast-copy | 160,060 |
| ramda | 134,873 |
| deepclone | 131,345 |
| fast-clone | 49,911 |
| clone | 75,780 |
| fast-copy (strict) | 61,556 |
| lodash.cloneDeep | 59,493 |
Very large number of properties with high amount of nesting, mainly objects and arrays
| Operations / second | |
|---|---|
| fast-copy | 304 |
| fast-clone | 236 |
| lodash.cloneDeep | 150 |
| deepclone | 148 |
| clone | 90 |
| fast-copy (strict) | 89 |
| ramda | 42 |
Objects that deeply reference themselves
| Operations / second | |
|---|---|
| fast-copy | 2,273,405 |
| deepclone | 1,299,928 |
| ramda | 951,721 |
| clone | 945,585 |
| lodash.cloneDeep | 887,775 |
| fast-copy (strict) | 630,221 |
| fast-clone | 0 (not supported) |
Custom constructors, React components, etc
| Operations / second | |
|---|---|
| fast-copy | 144,616 |
| clone | 68,880 |
| fast-clone | 66,233 |
| lodash.cloneDeep | 63,376 |
| ramda | 52,216 |
| deepclone | 28,394 |
| fast-copy (strict) | 21,033 |
Standard practice, clone the repo and yarn (or npm i) to get the dependencies. The following npm scripts are available:
build:files and build:typesrolluprimraf on the dist folderbuild and build:minified scriptssrc folder (also runs on dev script)lint script, but with auto-fixerlint, test:coverage, and dist scriptsprepublishOnly and release with new versionprepublishOnly and release with new beta versionprepublishOnly and simulate a new releasedevtest foldertest with code coverage calculation via nyctest but keep persistent watcher© 2010 - cnpmjs.org x YWFE | Home | YWFE