Giter Site home page Giter Site logo

rfdc's Introduction

rfdc

Really Fast Deep Clone

build status coverage js-standard-style

Usage

const clone = require('rfdc')()
clone({a: 1, b: {c: 2}}) // => {a: 1, b: {c: 2}}

API

require('rfdc')(opts = { proto: false, circles: false }) => clone(obj) => obj2

proto option

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).

To explain by way of code:

require('rfdc')({ proto: false })(Object.create({a: 1})) // => {}
require('rfdc')({ proto: true })(Object.create({a: 1})) // => {a: 1}

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.

default import

It is also possible to directly import the clone function with all options set to their default:

const clone = require("rfdc/default")
clone({a: 1, b: {c: 2}}) // => {a: 1, b: {c: 2}}

Types

rfdc clones all JSON types:

  • Object
  • Array
  • Number
  • String
  • null

With additional support for:

  • Date (copied)
  • undefined (copied)
  • Buffer (copied)
  • TypedArray (copied)
  • Map (copied)
  • Set (copied)
  • Function (referenced)
  • AsyncFunction (referenced)
  • GeneratorFunction (referenced)
  • arguments (copied to a normal object)

All other types have output values that match the output of JSON.parse(JSON.stringify(o)).

For instance:

const rfdc = require('rfdc')()
const err = Error()
err.code = 1
JSON.parse(JSON.stringify(e)) // {code: 1}
rfdc(e) // {code: 1}

JSON.parse(JSON.stringify({rx: /foo/})) // {rx: {}}
rfdc({rx: /foo/}) // {rx: {}}

Benchmarks

npm run bench
benchDeepCopy*100: 550.406ms
benchLodashCloneDeep*100: 1.479s
benchCloneDeep*100: 761.46ms
benchFastCopy*100: 800.38ms
benchFastestJsonCopy*100: 369.979ms // See note below
benchJsondiffpatchClone*100: 285.914ms // See note below
benchPlainObjectClone*100: 544ms
benchNanoCopy*100: 640.636ms
benchRamdaClone*100: 2.936s
benchJsonParseJsonStringify*100: 2.239s // JSON.parse(JSON.stringify(obj))
benchRfdc*100: 403.151ms
benchRfdcProto*100: 416.896ms
benchRfdcCircles*100: 453.706ms
benchRfdcCirclesProto*100: 404.127ms

It is true that jsondiffpatch.clone() from jsondiffpatch is the fastest in that benchmark run, but it cannot handle as many situations as rfdc can. Also, this and this benchmark result suggest that rfdc is faster in Firefox and Chrome at this different benchmark.

It is true that fastest-json-copy is faster than rfdc in this particular benchmark, but we have seen that, with different input, the opposite is true. Also, fastest-json-copy 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.

Tests

npm test
169 passing (342.514ms)

Coverage

npm run cov
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.js |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|

__proto__ own property copying

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.

For detailed write-up on how a way to handle this security-wise see https://www.fastify.io/docs/latest/Guides/Prototype-Poisoning/.

License

MIT

rfdc's People

Contributors

davidmarkclements avatar salmanm avatar jammspread avatar davidbludlow avatar nearw avatar johnathanludwig avatar operkh avatar bridgear avatar seldszar avatar johnjbarton avatar silverwind avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.