Quick Deep Clone (qdclone)
Quick deep cloning for JavaScript.
Unsafe for use on cyclical data structures. Does not copy functions as object properties.
Usage
Pass an object to clone as the first argument.
Pass true as the second argument to construct prototypes, empty or false to instantiate as plain objects.
Performance comes at a cost:
- no cyclic objects
- no protos that require arguments on construction (if
cast
flag istrue
) - no objects that shadow global proto properties
- no polluted base prototypes
Example
const qdclone = ; let x = str: 'str' num: 10 arr: 0 1 2; { thisstr = 'str'; thisnum = 10; thisarr = 0 1 2;} // deep clone a plain objectconsole; // deep clone a prototyped objectconsole; // deep clone a prototyped object as a plain objectconsole;
Benchmarks
Package | Time (ms) |
---|---|
deep-clone@3.0.3 | 1361 |
rfdc@1.1.4 | 1048 |
qdclone | 441 |
qdclone (* with casting) | 525 |
* only quick-deep-clone
supports casting prototypes
const performance = ; const cloners = 'deep-clone': 'rfdc': 'qdclone': ; const ITERATIONS = 10000; ; { let cloned; console; let benchStart = performance; for let i = 0; i < iterations; ++i cloned = clonerstypeobj ...args; console; return cloned;} { let x = str: 'str' num: 10 arr: 0 1 2 date: bool: false sub: str2: 'abcdefghijklmnopqrstuvwxyz' num2: 99999999999999 bool2: true arr2: -0000001 true 'asdf' 10000000001 1 2 3 'loooooooooooong' ; let x2 = x: JSON ; let x3 = x: JSON x2: JSON ; let xs = ; for let i = 0; i < 100; ++i xs; return X { thisx = x; thisx2 = x2; thisx3 = x3; thisxs = xs; } }
Contributing
Feel free to make changes and submit pull requests whenever.
License
Mousetouch uses the MIT license.