fast-copy and rfdc are both high-performance libraries designed to create deep copies of JavaScript objects. They aim to provide faster alternatives to naive recursive cloning or JSON-based serialization/deserialization, especially for complex nested data structures. Both avoid common pitfalls like circular references (though with different strategies) and support a wide range of JavaScript types beyond plain objects and arrays.
When you need to duplicate complex JavaScript objects without sharing references, JSON.parse(JSON.stringify(obj)) quickly falls shortβit fails on functions, undefined, Date objects, RegExp, Map, Set, and circular structures. Thatβs where dedicated deep clone libraries like fast-copy and rfdc come in. Both promise speed and correctness, but they take very different approaches. Letβs dig into how they behave in real code.
fast-copy supports a wide range of built-in JavaScript types:
Date, RegExpMap, SetArrayBuffer, DataView, and all typed arrays (Int8Array, Float64Array, etc.)null, undefined, functions (copied as-is, not deeply cloned)// fast-copy: Handles advanced types
import copy from 'fast-copy';
const original = {
date: new Date('2023-01-01'),
regex: /hello/g,
map: new Map([['key', 'value']]),
set: new Set([1, 2, 3]),
buffer: new Uint8Array([1, 2, 3]).buffer
};
const cloned = copy(original);
console.log(cloned.date instanceof Date); // true
console.log(cloned.map.get('key')); // 'value'
rfdc (Really Fast Deep Clone) focuses only on JSON-compatible types:
nullDate, RegExp, Map, Set, ArrayBuffer, functions, or undefined// rfdc: Throws on unsupported types
import rfdc from 'rfdc';
const clone = rfdc();
const original = {
date: new Date(),
regex: /test/
};
// This throws: "Cannot clone non-plain object"
// const cloned = clone(original);
π‘ If your data includes anything beyond basic objects and primitives,
rfdcwill fail.fast-copywonβt.
fast-copy uses a WeakMap to track seen objects and safely handles circular references by preserving the cycle in the clone.
// fast-copy: Circular reference handled gracefully
import copy from 'fast-copy';
const obj = { name: 'parent' };
obj.self = obj;
const cloned = copy(obj);
console.log(cloned.self === cloned); // true β cycle preserved
rfdc does not support circular references. Attempting to clone a circular structure throws an error immediately.
// rfdc: Circular reference throws
import rfdc from 'rfdc';
const clone = rfdc();
const obj = { name: 'parent' };
obj.self = obj;
// Throws: "Circular reference detected"
// const cloned = clone(obj);
This makes rfdc unsuitable for cloning arbitrary object graphs (e.g., DOM-like trees, state machines), while fast-copy remains robust.
rfdc is optimized for one thing: cloning plain objects as fast as possible. It avoids type checks and feature detection to minimize overhead. If your data is strictly JSON-like, itβs often the fastest option available.
// rfdc: Minimal overhead for plain data
import rfdc from 'rfdc';
const clone = rfdc();
const data = { users: [{ id: 1, name: 'Alice' }] };
const fastClone = clone(data); // Extremely fast
fast-copy pays a small performance cost to support more types and circular references. But for most real-world apps, the difference is negligibleβand the safety is worth it.
// fast-copy: Slightly slower but safer
import copy from 'fast-copy';
const data = { users: [{ id: 1, name: 'Alice' }] };
const safeClone = copy(data); // Still very fast, but more capable
In benchmarks with plain objects, rfdc typically wins. But once you add Date or Map, fast-copy is the only viable choice.
Both libraries have simple APIs, but differ slightly:
fast-copy exports a single function:
import copy from 'fast-copy';
const result = copy(source);
rfdc exports a factory function that returns a reusable clone function (to avoid repeated setup):
import rfdc from 'rfdc';
const clone = rfdc(); // Create once
const result = clone(source); // Reuse
This design lets rfdc optimize internal state per clone instance, but adds a tiny bit of ceremony.
rfdc when:// Example: Cloning API response data
const clone = rfdc();
const apiData = await fetch('/data').then(r => r.json());
const localCopy = clone(apiData); // Safe and fast
fast-copy when:Date, Map, or other non-plain objects// Example: Cloning complex app state
import copy from 'fast-copy';
const appState = {
user: { lastLogin: new Date() },
cache: new Map(),
config: { theme: 'dark' }
};
const backup = copy(appState); // Works without surprises
| Feature | fast-copy | rfdc |
|---|---|---|
| Plain objects | β Yes | β Yes (fastest) |
Date, RegExp | β Yes | β Throws |
Map, Set | β Yes | β Throws |
| Typed arrays | β Yes | β Throws |
| Circular refs | β Preserved safely | β Throws |
| Zero dependencies | β Yes | β Yes |
| API | copy(obj) | rfdc()() |
Think of rfdc as a race car: stripped down, blazing fast, but only drives on smooth tracks. fast-copy is a rugged SUV: slightly heavier, but handles mud, snow, and off-road terrain without breaking down.
If youβre certain your data is simple and clean, rfdc gives you peak performance. But in most real applicationsβwhere data comes from APIs, user input, or third-party librariesβfast-copyβs resilience and broader compatibility make it the safer, more practical choice.
Choose fast-copy if you need a zero-dependency, modern deep clone that handles a broad set of JavaScript types including Date, RegExp, Map, Set, ArrayBuffer, and typed arrays out of the box. It uses a WeakMap to safely handle circular references without throwing errors, making it suitable for general-purpose deep cloning in applications where data structure complexity is unknown or varied.
Choose rfdc if raw speed is your top priority and you're primarily working with plain objects, arrays, and primitive values. It's optimized for minimal overhead and fastest possible execution on standard JSON-like data, but it does not support advanced types like Map, Set, or Date and will throw an error if it encounters circular references.
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:
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.
createCopierCreate a custom copier based on the type-specific method overrides passed, as well as configuration options for how copies should be performed. 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';
import { LRUCache } from 'lru-cache';
const copyShallowStrict = createCopier({
createCache: () => new LRUCache(),
methods: {
array: (array) => [...array],
map: (map) => new Map(map.entries()),
object: (object) => ({ ...object }),
set: (set) => new Set(set.values()),
},
strict: true,
});
createCacheMethod that creates the internal cache in the Copier state. Defaults to creating a new
WeakMap instance.
methodsMethods used for copying specific object types. A list of the methods and which object types they handle:
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 => SetEach method has the following contract:
type InternalCopier<Value> = (value: Value, state: State) => Value;
interface State {
Constructor: any;
cache: WeakMap;
copier: InternalCopier<any>;
prototype: any;
}
cacheIf 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;
}
copiercopier 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 / prototypeBoth 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;
}
strictEnforces strict copying of properties, which includes properties that are not standard for that object. An example would be a named key on an array.
NOTE: This creates a copier that is significantly slower than "loose" mode, 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:
ArrayArrayBufferBoolean primitive wrappers (e.g., new Boolean(true))BlobBufferDataViewDateFloat32ArrayFloat64ArrayInt8ArrayInt16ArrayInt32ArrayMapNumber primitive wrappers (e.g., new Number(123))ObjectRegExpSetString primitive wrappers (e.g., new String('foo'))Uint8ArrayUint8ClampedArrayUint16ArrayUint32ArrayReact 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:
AsyncFunctionAsyncGeneratorBoolean primitivesErrorFunctionGeneratorGeneratorFunctionNumber primitivesNullPromiseString primitivesSymbolUndefinedWeakMapWeakSetCircular 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.
*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
ββββββββββββββββββββββ¬βββββββββββββββββ
β Name β Ops / sec β
ββββββββββββββββββββββΌβββββββββββββββββ€
β fast-copy β 4606103.720559 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β lodash.cloneDeep β 2575175.39241 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β clone β 2172921.6353 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β ramda β 1919715.448951 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β fast-clone β 1576610.693318 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β deepclone β 1173500.05884 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β fast-copy (strict) β 1049310.47701 β
ββββββββββββββββββββββ΄βββββββββββββββββ
Fastest was "fast-copy".
Large number of properties, values are a combination of primitives and complex objects
ββββββββββββββββββββββ¬ββββββββββββββββ
β Name β Ops / sec β
ββββββββββββββββββββββΌββββββββββββββββ€
β fast-copy β 235511.4532 β
ββββββββββββββββββββββΌββββββββββββββββ€
β deepclone β 142976.849406 β
ββββββββββββββββββββββΌββββββββββββββββ€
β clone β 125026.837887 β
ββββββββββββββββββββββΌββββββββββββββββ€
β ramda β 114216.98158 β
ββββββββββββββββββββββΌββββββββββββββββ€
β fast-clone β 111388.215547 β
ββββββββββββββββββββββΌββββββββββββββββ€
β fast-copy (strict) β 77683.900047 β
ββββββββββββββββββββββΌββββββββββββββββ€
β lodash.cloneDeep β 71343.431983 β
ββββββββββββββββββββββ΄ββββββββββββββββ
Fastest was "fast-copy".
Very large number of properties with high amount of nesting, mainly objects and arrays
Testing big data object...
ββββββββββββββββββββββ¬βββββββββββββ
β Name β Ops / sec β
ββββββββββββββββββββββΌβββββββββββββ€
β fast-copy β 325.548627 β
ββββββββββββββββββββββΌβββββββββββββ€
β fast-clone β 257.913886 β
ββββββββββββββββββββββΌβββββββββββββ€
β deepclone β 158.228042 β
ββββββββββββββββββββββΌβββββββββββββ€
β lodash.cloneDeep β 153.520966 β
ββββββββββββββββββββββΌβββββββββββββ€
β fast-copy (strict) β 126.027381 β
ββββββββββββββββββββββΌβββββββββββββ€
β clone β 123.383641 β
ββββββββββββββββββββββΌβββββββββββββ€
β ramda β 35.507959 β
ββββββββββββββββββββββ΄βββββββββββββ
Fastest was "fast-copy".
Testing circular object...
ββββββββββββββββββββββ¬βββββββββββββββββ
β Name β Ops / sec β
ββββββββββββββββββββββΌβββββββββββββββββ€
β fast-copy β 1344790.296938 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β deepclone β 1127781.641192 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β lodash.cloneDeep β 894679.711048 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β clone β 892911.50594 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β fast-copy (strict) β 821339.44828 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β ramda β 615222.946985 β
ββββββββββββββββββββββΌβββββββββββββββββ€
β fast-clone β 0 β
ββββββββββββββββββββββ΄βββββββββββββββββ
Fastest was "fast-copy".
Custom constructors, React components, etc
ββββββββββββββββββββββ¬βββββββββββββββ
β Name β Ops / sec β
ββββββββββββββββββββββΌβββββββββββββββ€
β fast-copy β 86875.694416 β
ββββββββββββββββββββββΌβββββββββββββββ€
β clone β 73525.671381 β
ββββββββββββββββββββββΌβββββββββββββββ€
β lodash.cloneDeep β 63280.563976 β
ββββββββββββββββββββββΌβββββββββββββββ€
β fast-clone β 52991.064016 β
ββββββββββββββββββββββΌβββββββββββββββ€
β ramda β 31770.652317 β
ββββββββββββββββββββββΌβββββββββββββββ€
β deepclone β 24253.795114 β
ββββββββββββββββββββββΌβββββββββββββββ€
β fast-copy (strict) β 19112.538416 β
ββββββββββββββββββββββ΄βββββββββββββββ
Fastest was "fast-copy".