Gathering detailed insights and metrics for fast-copy
Gathering detailed insights and metrics for fast-copy
Gathering detailed insights and metrics for fast-copy
Gathering detailed insights and metrics for fast-copy
npm install fast-copy
Release 3.0.2
Published on 16 Mar 2024
Release 3.0.1
Published on 18 Feb 2023
Release 3.0.0
Published on 03 Oct 2022
Release 3.0.0-beta.8
Published on 02 Oct 2022
Release 3.0.0-beta.7
Published on 30 Sept 2022
Release 3.0.0-beta.6
Published on 30 Sept 2022
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,134 Stars
183 Commits
31 Forks
9 Watching
6 Branches
12 Contributors
Updated on 09 Nov 2024
JavaScript (96.88%)
TypeScript (3.12%)
Cumulative downloads
Total Downloads
Last day
-7%
730,669
Compared to previous day
Last week
0.6%
4,101,579
Compared to previous week
Last month
13%
17,307,817
Compared to previous month
Last year
79.1%
159,567,068
Compared to previous year
34
A blazing fast deep object copier
1import copy from 'fast-copy'; 2import { deepEqual } from 'fast-equals'; 3 4const object = { 5 array: [123, { deep: 'value' }], 6 map: new Map([ 7 ['foo', {}], 8 [{ bar: 'baz' }, 'quz'], 9 ]), 10}; 11 12const copiedObject = copy(object); 13 14console.log(copiedObject === object); // false 15console.log(deepEqual(copiedObject, object)); // true
copy
Deeply copy the object passed.
1import copy from 'fast-copy'; 2 3const copied = copy({ foo: 'bar' });
copyStrict
Deeply copy the object passed, but with additional strictness when replicating the original object:
1import { copyStrict } from 'fast-copy'; 2 3const object = { foo: 'bar' }; 4object.nonEnumerable = Object.defineProperty(object, 'bar', { 5 enumerable: false, 6 value: 'baz', 7}); 8 9const 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.
1import { createCopier } from 'fast-copy'; 2 3const copyShallow = createCopier({ 4 array: (array) => [...array], 5 map: (map) => new Map(map.entries()), 6 object: (object) => ({ ...object }), 7 set: (set) => new Set(set.values()), 8});
Each internal copier method has the following contract:
1type InternalCopier<Value> = (value: Value, state: State) => Value; 2 3interface State { 4 Constructor: any; 5 cache: WeakMap; 6 copier: InternalCopier<any>; 7 prototype: any; 8}
Any method overriding the defaults must maintain this contract.
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 constructorregExp
=> RegExp
set
=> Set
cache
If you want to maintain circular reference handling, then you'll need the methods to handle cache population for future lookups:
1function shallowlyCloneArray<Value extends any[]>( 2 value: Value, 3 state: State 4): Value { 5 const clone = [...value]; 6 7 state.cache.set(value, clone); 8 9 return clone; 10}
copier
copier
is provided for recursive calls with deeply-nested objects.
1function deeplyCloneArray<Value extends any[]>( 2 value: Value, 3 state: State 4): Value { 5 const clone = []; 6 7 state.cache.set(value, clone); 8 9 value.forEach((item) => state.copier(item, state)); 10 11 return clone; 12}
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.
1function deeplyCloneSubclassArray<Value extends CustomArray>( 2 value: Value, 3 state: State 4): Value { 5 const clone = new state.Constructor(); 6 7 state.cache.set(value, clone); 8 9 value.forEach((item) => clone.push(item)); 10 11 return clone; 12} 13 14function deeplyCloneCustomObject<Value extends CustomObject>( 15 value: Value, 16 state: State 17): Value { 18 const clone = Object.create(state.prototype); 19 20 state.cache.set(value, clone); 21 22 Object.entries(value).forEach(([k, v]) => (clone[k] = v)); 23 24 return clone; 25}
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.
1const createStrictClone = (value, clone) =>
2 Object.getOwnPropertyNames(value).reduce(
3 (clone, property) =>
4 Object.defineProperty(
5 clone,
6 property,
7 Object.getOwnPropertyDescriptor(value, property) || {
8 configurable: true,
9 enumerable: true,
10 value: clone[property],
11 writable: true,
12 }
13 ),
14 clone
15 );
16
17const copyStrictShallow = createStrictCopier({
18 array: (array) => createStrictClone(array, []),
19 map: (map) => createStrictClone(map, new Map(map.entries())),
20 object: (object) => createStrictClone(object, {}),
21 set: (set) => createStrictClone(set, new Set(set.values())),
22});
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.
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
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:
AsyncFunction
Boolean
primitivesError
Function
GeneratorFunction
Number
primitivesNull
Promise
String
primitivesSymbol
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
.
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
objectWhile 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, but there is the possibility of inaccuracy if the contract differs.
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.
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 |
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 |
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 |
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) |
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 |
Standard practice, clone the repo and yarn
(or npm i
) to get the dependencies. The following npm scripts are available:
build:esm
, build:cjs
, build:umd
, and build:min
scriptsrimraf
on the dist
folderclean
and build
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 releasedev
test
foldertest
with code coverage calculation via nyc
test
but keep persistent watchertsc
on the codebaseNo vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/17 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
20 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More