Copy prototype properties as well as own properties into the new object.
It's marginally faster to allow enumerable properties on the prototype
to be copied into the cloned object (not onto it's prototype, directly onto the object).
Setting proto to true will provide an additional 2% performance boost.
circles option
Keeping track of circular references will slow down performance with an
additional 25% overhead. Even if an object doesn't have any circular references,
the tracking overhead is the cost. By default if an object with a circular
reference is passed to rfdc, it will throw (similar to how JSON.stringify
would throw).
Use the circles option to detect and preserve circular references in the
object. If performance is important, try removing the circular reference from
the object (set to undefined) and then add it back manually after cloning
instead of using this option.
constructorHandlers option
Sometimes consumers may want to add custom clone behaviour for particular classes
(for example RegExp or ObjectId, which aren't supported out-of-the-box).
This can be done by passing constructorHandlers, which takes an array of tuples,
where the first item is the class to match, and the second item is a function that
takes the input and returns a cloned output:
NOTE: For performance reasons, the handlers will only match an instance of the
exact class (not a subclass). Subclasses will need to be added separately if they
also need special clone behaviour.
default import
It is also possible to directly import the clone function with all options set
to their default:
It is true that fastest-json-copy may be faster, BUT it has such huge limitations that it is rarely useful. For example, it treats things like Date and Map instances the same as empty {}. It can't handle circular references. plain-object-clone is also really limited in capability.
rfdc works the same way as Object.assign when it comes to copying ['__proto__'] (e.g. when
an object has an own property key called 'proto'). It results in the target object
prototype object being set per the value of the ['__proto__'] own property.