fast-copy

一个快速的深度对象拷贝器。(A blazing fast deep object copier)

  • 所有者: planttheidea/fast-copy
  • 平台: Linux, Mac, Windows
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

fast-copy

A blazing fast deep object copier

Table of contents

Usage

import copy from "fast-copy";
import { deepEqual } from "fast-equals";

const object = {
  array: [123, { deep: "value" }],
  map: new Map([["foo", {}], [{ bar: "baz" }, "quz"]])
};

const copiedObject = copy(object);

console.log(copiedObject === object); // false
console.log(deepEqual(copiedObject, object)); // true

Options

isStrict

Starting in 2.0.0, you can use the isStrict option to copy the object based on strict standards, meaning:

  • Properties retain their original property descriptor
  • Non-enumerable properties are copied
  • Non-standard properties (e.g., keys on an Array object) are copied

This is significantly slower, so you should only use this if you believe it necessary.

console.log(copy(object, { isStrict: true }));

NOTE: This option is also aliased as copy.strict.

console.log(copy.strict(object));

realm

Under the hood, fast-copy uses instanceof to determine object types, which can cause false negatives when used in combination with iframe-based objects. To handle this edge case, you can pass the realm in options, which identifies which realm the object comes from and will use that realm to drive both comparisons and constructors for the copies.

<iframe srcdoc="<script>var arr = ['foo', 'bar'];</script>"></iframe>
const iframe = document.querySelector("iframe");
const arr = iframe.contentWindow.arr;

console.log(copy(arr, { realm: iframe.contentWindow })); // ['foo', 'bar']

Types supported

The following object types are deeply cloned when they are either properties on the object passed, or the object itself:

  • Array
  • ArrayBuffer
  • Buffer
  • DataView
  • Date
  • Float32Array
  • Float64Array
  • Int8Array
  • Int16Array
  • Int32Array
  • Map
  • Object
  • RegExp
  • Set
  • Uint8Array
  • Uint8ClampedArray
  • Uint16Array
  • Uint32Array
  • React components
  • Custom constructors

The following object types are copied directly, as they are either primitives, cannot be cloned, or the common use-case implementation does not expect cloning:

  • AsyncFunction
  • Boolean
  • Error
  • Function
  • GeneratorFunction
  • Number
  • Null
  • Promise
  • String
  • Symbol
  • Undefined
  • WeakMap
  • WeakSet

Circular objects are supported out of the box as well. By default a cache based on WeakSet is used, but if WeakSet is not available then a standard Object fallback is used. The benchmarks quoted below are based on use of WeakSet.

Benchmarks

Simple objects

Small number of properties, all values are primitives

主要指標

概覽
名稱與所有者planttheidea/fast-copy
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台Linux, Mac, Windows
許可證MIT License
所有者活动
創建於2018-05-11 10:25:00
推送於2024-03-28 20:50:46
最后一次提交2024-03-16 09:42:59
發布數31
最新版本名稱v3.0.2 (發布於 2024-03-16 09:41:35)
第一版名稱v1.2.4 (發布於 2019-01-26 07:27:50)
用户参与
星數1.1k
關注者數8
派生數25
提交數183
已啟用問題?
問題數24
打開的問題數1
拉請求數69
打開的拉請求數3
關閉的拉請求數4
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?