rfdc

中文文档
1.4.1发布于 2024/06/12MITTypeScriptCommonJS零依赖近 30 天下载 2.2亿

Really Fast Deep Clone

原始使用文档
文档目录10 节
  1. Usage
  2. API
  3. require('rfdc')(opts = { proto: false, circles: false, cons…
  4. default import
  5. Types
  6. Benchmarks
  7. Tests
  8. Coverage
  9. __proto__ own property copying
  10. License

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, constructorHandlers: [] }) => 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.

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:

const clone = require('rfdc')({
  constructorHandlers: [
    [RegExp, (o) => new RegExp(o)],
  ]
})

clone({r: /foo/}) // => {r: /foo/}

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:

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: 671.675ms
benchLodashCloneDeep*100: 1.574s
benchCloneDeep*100: 936.792ms
benchFastCopy*100: 822.668ms
benchFastestJsonCopy*100: 363.898ms // See note below
benchPlainObjectClone*100: 556.635ms
benchNanoCopy*100: 770.234ms
benchRamdaClone*100: 2.695s
benchJsonParseJsonStringify*100: 2.290s // JSON.parse(JSON.stringify(obj))
benchRfdc*100: 412.818ms
benchRfdcProto*100: 424.076ms
benchRfdcCircles*100: 443.357ms
benchRfdcCirclesProto*100: 465.053ms

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.

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 共发布 15 个版本,首次发布于 2018/05/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.4.1latest2024/06/122 年前
1.4.02024/06/122 年前
1.3.12024/01/192 年前
1.3.02021/03/155 年前
1.2.02021/01/165 年前
1.1.42019/05/137 年前

关于 rfdc 的常见问题

rfdc 是什么?

Really Fast Deep Clone

如何安装 rfdc?

在项目目录运行 npm install rfdc,或使用 pnpm add / yarn add / bun add。

rfdc 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

rfdc 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

rfdc 可以商用吗?

rfdc 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

rfdc 的下载量有多少?

根据 npm 官方下载统计,rfdc 在 2026/10/05 – 2026/11/03 这 30 天内被下载 219,943,326 次,日均约 7,331,444 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

rfdc 还在维护吗?

最新版本 1.4.1 发布于 2024/06/12(2 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/05/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。