fclone

2018-10-13 admin

fclone是什么

什么是fclone,Clone objects by dropping circular references

fclone使用教程帮助文档

FClone

Clone objects by dropping circular references

Build Status

This module clones a Javascript object in safe mode (eg: drops circular values) recursively. Circular values are replaced with a string: '[Circular]'.

Ideas from tracker1/safe-clone-deep. I improved the workflow a bit by:

  • refactoring the code (complete rewrite)
  • fixing node 6+
  • micro optimizations
  • use of Array.isArray and Buffer.isBuffer

Node 0.10 compatible, distributed files are translated to es2015.

Installation

npm install fclone
# or
bower install fclone

Usage

const fclone = require('fclone');

let a = {c: 'hello'};
a.b = a;

let o = fclone(a);

console.log(o);
// outputs: { c: 'hello', b: '[Circular]' }

//JSON.stringify is now safe
console.log(JSON.stringify(o));

Benchmarks

Some benchs:

# Clone
fclone (not a string) x 14,121 ops/sec ±0.75% (89 runs sampled)
clone (not a string) x 9,293 ops/sec ±0.93% (90 runs sampled)
deepcopy (not a string) x 5,375 ops/sec ±0.73% (92 runs sampled)

# Stringify
fclone + json.stringify x 8,289 ops/sec ±0.74% (90 runs sampled)
fast-safe-stringify x 8,241 ops/sec ±0.48% (92 runs sampled)
util.inspect (outputs a string) x 2,115 ops/sec ±0.84% (89 runs sampled)
jsan x 5,090 ops/sec ±0.65% (92 runs sampled)
circularjson x 4,471 ops/sec ±0.67% (92 runs sampled)
json-stringify-safe x 7,150 ops/sec ±0.97% (91 runs sampled)
Fastest is fclone (not a string)

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-42593.html

文章标题:fclone

回到顶部