版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 }) => 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. ### `default` import
  34. It is also possible to directly import the clone function with all options set
  35. to their default:
  36. ```js
  37. const clone = require("rfdc/default")
  38. clone({a: 1, b: {c: 2}}) // => {a: 1, b: {c: 2}}
  39. ```
  40. ### Types
  41. `rfdc` clones all JSON types:
  42. * `Object`
  43. * `Array`
  44. * `Number`
  45. * `String`
  46. * `null`
  47. With additional support for:
  48. * `Date` (copied)
  49. * `undefined` (copied)
  50. * `Buffer` (copied)
  51. * `TypedArray` (copied)
  52. * `Map` (copied)
  53. * `Set` (copied)
  54. * `Function` (referenced)
  55. * `AsyncFunction` (referenced)
  56. * `GeneratorFunction` (referenced)
  57. * `arguments` (copied to a normal object)
  58. All other types have output values that match the output
  59. of `JSON.parse(JSON.stringify(o))`.
  60. For instance:
  61. ```js
  62. const rfdc = require('rfdc')()
  63. const err = Error()
  64. err.code = 1
  65. JSON.parse(JSON.stringify(e)) // {code: 1}
  66. rfdc(e) // {code: 1}
  67. JSON.parse(JSON.stringify({rx: /foo/})) // {rx: {}}
  68. rfdc({rx: /foo/}) // {rx: {}}
  69. ```
  70. ## Benchmarks
  71. ```sh
  72. npm run bench
  73. ```
  74. ```
  75. benchDeepCopy*100: 457.568ms
  76. benchLodashCloneDeep*100: 1230.773ms
  77. benchCloneDeep*100: 655.208ms
  78. benchFastCopy*100: 747.017ms
  79. benchRfdc*100: 281.018ms
  80. benchRfdcProto*100: 277.265ms
  81. benchRfdcCircles*100: 328.148ms
  82. benchRfdcCirclesProto*100: 323.004ms
  83. ```
  84. ## Tests
  85. ```sh
  86. npm test
  87. ```
  88. ```
  89. 169 passing (342.514ms)
  90. ```
  91. ### Coverage
  92. ```sh
  93. npm run cov
  94. ```
  95. ```
  96. ----------|----------|----------|----------|----------|-------------------|
  97. File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
  98. ----------|----------|----------|----------|----------|-------------------|
  99. All files | 100 | 100 | 100 | 100 | |
  100. index.js | 100 | 100 | 100 | 100 | |
  101. ----------|----------|----------|----------|----------|-------------------|
  102. ```
  103. ## License
  104. MIT