855ae658b5361d2e852fdbaea89336775a64bd9fe2b11db885b99695d72b5a0898382c805f8e6d3e2e69b13498d521fddcd1b8b35103f5d759b4688c8c8459 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # rfdc
  2. Really Fast Deep Clone
  3. [![build status](https://img.shields.io/travis/davidmarkclements/rfdc.svg)](https://travis-ci.org/davidmarkclements/rfdc)
  4. [![coverage](https://img.shields.io/codecov/c/github/davidmarkclements/rfdc.svg)](https://codecov.io/gh/davidmarkclements/rfdc)
  5. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
  6. ## Usage
  7. ```js
  8. const clone = require('rfdc')()
  9. clone({a: 1, b: {c: 2}}) // => {a: 1, b: {c: 2}}
  10. ```
  11. ## API
  12. ### `require('rfdc')(opts = { proto: false, circles: false, constructorHandlers: [] }) => clone(obj) => obj2`
  13. #### `proto` option
  14. Copy prototype properties as well as own properties into the new object.
  15. It's marginally faster to allow enumerable properties on the prototype
  16. to be copied into the cloned object (not onto it's prototype, directly onto the object).
  17. To explain by way of code:
  18. ```js
  19. require('rfdc')({ proto: false })(Object.create({a: 1})) // => {}
  20. require('rfdc')({ proto: true })(Object.create({a: 1})) // => {a: 1}
  21. ```
  22. Setting `proto` to `true` will provide an additional 2% performance boost.
  23. #### `circles` option
  24. Keeping track of circular references will slow down performance with an
  25. additional 25% overhead. Even if an object doesn't have any circular references,
  26. the tracking overhead is the cost. By default if an object with a circular
  27. reference is passed to `rfdc`, it will throw (similar to how `JSON.stringify` \
  28. would throw).
  29. Use the `circles` option to detect and preserve circular references in the
  30. object. If performance is important, try removing the circular reference from
  31. the object (set to `undefined`) and then add it back manually after cloning
  32. instead of using this option.
  33. #### `constructorHandlers` option
  34. Sometimes consumers may want to add custom clone behaviour for particular classes
  35. (for example `RegExp` or `ObjectId`, which aren't supported out-of-the-box).
  36. This can be done by passing `constructorHandlers`, which takes an array of tuples,
  37. where the first item is the class to match, and the second item is a function that
  38. takes the input and returns a cloned output:
  39. ```js
  40. const clone = require('rfdc')({
  41. constructorHandlers: [
  42. [RegExp, (o) => new RegExp(o)],
  43. ]
  44. })
  45. clone({r: /foo/}) // => {r: /foo/}
  46. ```
  47. **NOTE**: For performance reasons, the handlers will only match an instance of the
  48. *exact* class (not a subclass). Subclasses will need to be added separately if they
  49. also need special clone behaviour.
  50. ### `default` import
  51. It is also possible to directly import the clone function with all options set
  52. to their default:
  53. ```js
  54. const clone = require("rfdc/default")
  55. clone({a: 1, b: {c: 2}}) // => {a: 1, b: {c: 2}}
  56. ```
  57. ### Types
  58. `rfdc` clones all JSON types:
  59. * `Object`
  60. * `Array`
  61. * `Number`
  62. * `String`
  63. * `null`
  64. With additional support for:
  65. * `Date` (copied)
  66. * `undefined` (copied)
  67. * `Buffer` (copied)
  68. * `TypedArray` (copied)
  69. * `Map` (copied)
  70. * `Set` (copied)
  71. * `Function` (referenced)
  72. * `AsyncFunction` (referenced)
  73. * `GeneratorFunction` (referenced)
  74. * `arguments` (copied to a normal object)
  75. All other types have output values that match the output
  76. of `JSON.parse(JSON.stringify(o))`.
  77. For instance:
  78. ```js
  79. const rfdc = require('rfdc')()
  80. const err = Error()
  81. err.code = 1
  82. JSON.parse(JSON.stringify(e)) // {code: 1}
  83. rfdc(e) // {code: 1}
  84. JSON.parse(JSON.stringify({rx: /foo/})) // {rx: {}}
  85. rfdc({rx: /foo/}) // {rx: {}}
  86. ```
  87. ## Benchmarks
  88. ```sh
  89. npm run bench
  90. ```
  91. ```
  92. benchDeepCopy*100: 671.675ms
  93. benchLodashCloneDeep*100: 1.574s
  94. benchCloneDeep*100: 936.792ms
  95. benchFastCopy*100: 822.668ms
  96. benchFastestJsonCopy*100: 363.898ms // See note below
  97. benchPlainObjectClone*100: 556.635ms
  98. benchNanoCopy*100: 770.234ms
  99. benchRamdaClone*100: 2.695s
  100. benchJsonParseJsonStringify*100: 2.290s // JSON.parse(JSON.stringify(obj))
  101. benchRfdc*100: 412.818ms
  102. benchRfdcProto*100: 424.076ms
  103. benchRfdcCircles*100: 443.357ms
  104. benchRfdcCirclesProto*100: 465.053ms
  105. ```
  106. It is true that [fastest-json-copy](https://www.npmjs.com/package/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](https://www.npmjs.com/package/plain-object-clone) is also really limited in capability.
  107. ## Tests
  108. ```sh
  109. npm test
  110. ```
  111. ```
  112. 169 passing (342.514ms)
  113. ```
  114. ### Coverage
  115. ```sh
  116. npm run cov
  117. ```
  118. ```
  119. ----------|----------|----------|----------|----------|-------------------|
  120. File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
  121. ----------|----------|----------|----------|----------|-------------------|
  122. All files | 100 | 100 | 100 | 100 | |
  123. index.js | 100 | 100 | 100 | 100 | |
  124. ----------|----------|----------|----------|----------|-------------------|
  125. ```
  126. ### `__proto__` own property copying
  127. `rfdc` works the same way as `Object.assign` when it comes to copying `['__proto__']` (e.g. when
  128. an object has an own property key called '__proto__'). It results in the target object
  129. prototype object being set per the value of the `['__proto__']` own property.
  130. For detailed write-up on how a way to handle this security-wise see https://www.fastify.io/docs/latest/Guides/Prototype-Poisoning/.
  131. ## License
  132. MIT