What is fclone?
The fclone npm package is designed to provide deep cloning functionality for JavaScript objects. It ensures that complex objects, including those with circular references, are cloned accurately without losing any data or structure.
What are fclone's main functionalities?
Deep Cloning
This feature allows you to create a deep clone of an object, ensuring that nested objects are also cloned rather than referenced.
const fclone = require('fclone');
const original = { a: 1, b: { c: 2 } };
const clone = fclone(original);
console.log(clone); // { a: 1, b: { c: 2 } }
Handling Circular References
fclone can handle objects with circular references, ensuring that the clone maintains the same structure without causing infinite loops.
const fclone = require('fclone');
const obj = { a: 1 };
obj.self = obj;
const clone = fclone(obj);
console.log(clone); // { a: 1, self: [Circular] }
Cloning Arrays
This feature allows you to deep clone arrays, ensuring that nested objects within the array are also cloned.
const fclone = require('fclone');
const original = [1, 2, { a: 3 }];
const clone = fclone(original);
console.log(clone); // [1, 2, { a: 3 }]
Other packages similar to fclone
lodash
Lodash is a utility library that provides a wide range of functions, including deep cloning with `_.cloneDeep`. It is more feature-rich compared to fclone, offering many other utilities for data manipulation.
rfdc
rfdc (Really Fast Deep Clone) is a lightweight and fast deep cloning library. It is designed for performance and is faster than fclone, but it may not handle all edge cases as comprehensively.
clone-deep
clone-deep is another library focused on deep cloning. It offers similar functionality to fclone but with additional options for customizing the cloning process.
FClone
Clone objects by dropping circular references
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
bower install fclone
Usage
const fclone = require('fclone');
let a = {c: 'hello'};
a.b = a;
let o = fclone(a);
console.log(o);
console.log(JSON.stringify(o));
Benchmarks
Some benchs:
fclone x 17,081 ops/sec ±0.71% (79 runs sampled)
fclone + json.stringify x 9,433 ops/sec ±0.91% (81 runs sampled)
util.inspect (outputs a string) x 2,498 ops/sec ±0.77% (90 runs sampled)
jsan x 5,379 ops/sec ±0.82% (91 runs sampled)
circularjson x 4,719 ops/sec ±1.16% (91 runs sampled)
deepcopy x 5,478 ops/sec ±0.77% (86 runs sampled)
json-stringify-safe x 5,828 ops/sec ±1.30% (84 runs sampled)
clone x 8,713 ops/sec ±0.68% (88 runs sampled)
Fastest is util.format (outputs a string)