package/package.json000644 0000002602 3560116604 011546 0ustar00000000 000000 { "name": "rfdc", "version": "1.1.4", "description": "Really Fast Deep Clone", "main": "index.js", "scripts": { "test": "tap -R min test && npm run lint", "bench": "node benchmark", "lint": "standard --fix", "cov": "tap --100 test", "cov-ui": "tap --coverage-report=html test", "ci": "standard && tap --100 --coverage-report=text-lcov test | codecov --pipe" }, "keywords": [ "object", "obj", "properties", "clone", "copy", "deep", "recursive", "key", "keys", "values", "prop", "deep-clone", "deepclone", "deep-copy", "deepcopy", "fast", "performance", "performant", "fastclone", "fastcopy", "fast-clone", "fast-deep-clone", "fast-copy", "fast-deep-copy" ], "author": "David Mark Clements ", "license": "MIT", "devDependencies": { "codecov": "^3.4.0", "deep-copy": "^1.4.2", "fast-copy": "^1.2.1", "fastbench": "^1.0.1", "lodash.clonedeep": "^4.5.0", "standard": "^11.0.1", "tap": "^12.0.1" }, "directories": { "test": "test" }, "dependencies": {}, "repository": { "type": "git", "url": "git+https://github.com/davidmarkclements/rfdc.git" }, "bugs": { "url": "https://github.com/davidmarkclements/rfdc/issues" }, "homepage": "https://github.com/davidmarkclements/rfdc#readme" } package/index.js000644 0000006744 3560116604 010740 0ustar00000000 000000 'use strict' module.exports = rfdc function rfdc (opts) { opts = opts || {} if (opts.circles) return rfdcCircles(opts) return opts.proto ? cloneProto : clone function cloneArray (a, fn) { var keys = Object.keys(a) var a2 = new Array(keys.length) for (var i = 0; i < keys.length; i++) { var k = keys[i] var cur = a[k] if (typeof cur !== 'object' || cur === null) { a2[k] = cur } else if (cur instanceof Date) { a2[k] = new Date(cur) } else { a2[k] = fn(cur) } } return a2 } function clone (o) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, clone) var o2 = {} for (var k in o) { if (Object.hasOwnProperty.call(o, k) === false) continue var cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) } else { o2[k] = clone(cur) } } return o2 } function cloneProto (o) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, cloneProto) var o2 = {} for (var k in o) { var cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) } else { o2[k] = cloneProto(cur) } } return o2 } } function rfdcCircles (opts) { var refs = [] var refsNew = [] return opts.proto ? cloneProto : clone function cloneArray (a, fn) { var keys = Object.keys(a) var a2 = new Array(keys.length) for (var i = 0; i < keys.length; i++) { var k = keys[i] var cur = a[k] if (typeof cur !== 'object' || cur === null) { a2[k] = cur } else if (cur instanceof Date) { a2[k] = new Date(cur) } else { var index = refs.indexOf(cur) if (index !== -1) { a2[k] = refsNew[index] } else { a2[k] = fn(cur) } } } return a2 } function clone (o) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, clone) var o2 = {} refs.push(o) refsNew.push(o2) for (var k in o) { if (Object.hasOwnProperty.call(o, k) === false) continue var cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) } else { var i = refs.indexOf(cur) if (i !== -1) { o2[k] = refsNew[i] } else { o2[k] = clone(cur) } } } refs.pop() refsNew.pop() return o2 } function cloneProto (o) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, cloneProto) var o2 = {} refs.push(o) refsNew.push(o2) for (var k in o) { var cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) } else { var i = refs.indexOf(cur) if (i !== -1) { o2[k] = refsNew[i] } else { o2[k] = cloneProto(cur) } } } refs.pop() refsNew.pop() return o2 } } package/LICENSE000644 0000002115 3560116604 010264 0ustar00000000 000000 Copyright 2019 "David Mark Clements " Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/readme.md000644 0000006425 3560116604 011046 0ustar00000000 000000 # rfdc Really Fast Deep Clone [![build status](https://img.shields.io/travis/davidmarkclements/rfdc.svg)](https://travis-ci.org/davidmarkclements/rfdc) [![coverage](https://img.shields.io/codecov/c/github/davidmarkclements/rfdc.svg)](https://codecov.io/gh/davidmarkclements/rfdc) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) ## Usage ```js const clone = require('rfdc')() clone({a: 1, b: {c: 2}}) // => {a: 1, b: {c: 2}} ``` ## API ### `require('rfdc')(opts = { proto: false, circles: false }) => 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: ```js 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. ### Types `rdfc` clones all JSON types: * `Object` * `Array` * `Number` * `String` * `null` With additional support for: * `Date` (copied) * `undefined` (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: ```js const rdfc = require('rdfc')() const err = Error() err.code = 1 JSON.parse(JSON.stringify(e)) // {code: 1} rdfc(e) // {code: 1} JSON.parse(JSON.stringify(new Uint8Array([1, 2, 3]))) // {'0': 1, '1': 2, '2': 3 } rdfc(new Uint8Array([1, 2, 3])) // {'0': 1, '1': 2, '2': 3 } JSON.parse(JSON.stringify({rx: /foo/})) // {rx: {}} rdfc({rx: /foo/}) // {rx: {}} ``` ## Benchmarks ```sh npm run bench ``` ``` benchDeepCopy*100: 549.618ms benchLodashCloneDeep*100: 1461.134ms benchFastCopy*100: 878.146ms benchRfdc*100: 323.899ms benchRfdcProto*100: 314.136ms benchRfdcCircles*100: 384.561ms benchRfdcCirclesProto*100: 381.775ms ``` ## Tests ```sh npm test ``` ``` 169 passing (342.514ms) ``` ### Coverage ```sh npm run cov ``` ``` ----------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ----------|----------|----------|----------|----------|-------------------| All files | 100 | 100 | 100 | 100 | | index.js | 100 | 100 | 100 | 100 | | ----------|----------|----------|----------|----------|-------------------| ``` ## License MIT package/test/index.js000644 0000015311 3560116604 011705 0ustar00000000 000000 'use strict' const { test } = require('tap') const rfdc = require('..') const clone = rfdc() const cloneProto = rfdc({proto: true}) const cloneCircles = rfdc({circles: true}) const cloneCirclesProto = rfdc({circles: true, proto: true}) types(clone, 'default') types(cloneProto, 'proto option') types(cloneCircles, 'circles option') types(cloneCirclesProto, 'circles and proto option') test('default – does not copy proto properties', async ({is}) => { is(clone(Object.create({a: 1})).a, undefined, 'value not copied') }) test('proto option – copies enumerable proto properties', async ({is}) => { is(cloneProto(Object.create({a: 1})).a, 1, 'value copied') }) test('circles option - circular object', async ({same, is, isNot}) => { const o = {nest: {a: 1, b: 2}} o.circular = o same(cloneCircles(o), o, 'same values') isNot(cloneCircles(o), o, 'different objects') isNot(cloneCircles(o).nest, o.nest, 'different nested objects') const c = cloneCircles(o) is(c.circular, c, 'circular references point to copied parent') isNot(c.circular, o, 'circular references do not point to original parent') }) test('circles option – deep circular object', async ({same, is, isNot}) => { const o = {nest: {a: 1, b: 2}} o.nest.circular = o same(cloneCircles(o), o, 'same values') isNot(cloneCircles(o), o, 'different objects') isNot(cloneCircles(o).nest, o.nest, 'different nested objects') const c = cloneCircles(o) is(c.nest.circular, c, 'circular references point to copied parent') isNot(c.nest.circular, o, 'circular references do not point to original parent') }) test('circles option alone – does not copy proto properties', async ({is}) => { is(cloneCircles(Object.create({a: 1})).a, undefined, 'value not copied') }) test('circles and proto option – copies enumerable proto properties', async ({is}) => { is(cloneCirclesProto(Object.create({a: 1})).a, 1, 'value copied') }) test('circles and proto option - circular object', async ({same, is, isNot}) => { const o = {nest: {a: 1, b: 2}} o.circular = o same(cloneCirclesProto(o), o, 'same values') isNot(cloneCirclesProto(o), o, 'different objects') isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects') const c = cloneCirclesProto(o) is(c.circular, c, 'circular references point to copied parent') isNot(c.circular, o, 'circular references do not point to original parent') }) test('circles and proto option – deep circular object', async ({same, is, isNot}) => { const o = {nest: {a: 1, b: 2}} o.nest.circular = o same(cloneCirclesProto(o), o, 'same values') isNot(cloneCirclesProto(o), o, 'different objects') isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects') const c = cloneCirclesProto(o) is(c.nest.circular, c, 'circular references point to copied parent') isNot(c.nest.circular, o, 'circular references do not point to original parent') }) test('circles and proto option – deep circular array', async ({same, is, isNot}) => { const o = { nest: [1, 2] } o.nest.push(o) same(cloneCirclesProto(o), o, 'same values') isNot(cloneCirclesProto(o), o, 'different objects') isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects') const c = cloneCirclesProto(o) is(c.nest[2], c, 'circular references point to copied parent') isNot(c.nest[2], o, 'circular references do not point to original parent') }) function types (clone, label) { test(label + ' – number', async ({is}) => { is(clone(42), 42, 'same value') }) test(label + ' – string', async ({is}) => { is(clone('str'), 'str', 'same value') }) test(label + ' – boolean', async ({is}) => { is(clone(true), true, 'same value') }) test(label + ' – function', async ({is}) => { const fn = () => {} is(clone(fn), fn, 'same function') }) test(label + ' – async function', async ({is}) => { const fn = async () => {} is(clone(fn), fn, 'same function') }) test(label + ' – generator function', async ({is}) => { const fn = function * () {} is(clone(fn), fn, 'same function') }) test(label + ' – date', async ({is, isNot}) => { const date = new Date() is(+clone(date), +date, 'same value') isNot(clone(date), date, 'different object') }) test(label + ' – null', async ({is}) => { is(clone(null), null, 'same value') }) test(label + ' – shallow object', async ({same, isNot}) => { const o = {a: 1, b: 2} same(clone(o), o, 'same values') isNot(clone(o), o, 'different object') }) test(label + ' – shallow array', async ({same, isNot}) => { const o = [1, 2] same(clone(o), o, 'same values') isNot(clone(o), o, 'different arrays') }) test(label + ' – deep object', async ({same, isNot}) => { const o = {nest: {a: 1, b: 2}} same(clone(o), o, 'same values') isNot(clone(o), o, 'different objects') isNot(clone(o).nest, o.nest, 'different nested objects') }) test(label + ' – deep array', async ({same, isNot}) => { const o = [ {a: 1, b: 2}, [3] ] same(clone(o), o, 'same values') isNot(clone(o), o, 'different arrays') isNot(clone(o)[0], o[0], 'different array elements') isNot(clone(o)[1], o[1], 'different array elements') }) test(label + ' – nested number', async ({is}) => { is(clone({a: 1}).a, 1, 'same value') }) test(label + ' – nested string', async ({is}) => { is(clone({s: 'str'}).s, 'str', 'same value') }) test(label + ' – nested boolean', async ({is}) => { is(clone({b: true}).b, true, 'same value') }) test(label + ' – nested function', async ({is}) => { const fn = () => {} is(clone({fn}).fn, fn, 'same function') }) test(label + ' – nested async function', async ({is}) => { const fn = async () => {} is(clone({fn}).fn, fn, 'same function') }) test(label + ' – nested generator function', async ({is}) => { const fn = function * () {} is(clone({fn}).fn, fn, 'same function') }) test(label + ' – nested date', async ({is, isNot}) => { const date = new Date() is(+clone({d: date}).d, +date, 'same value') isNot(clone({d: date}).d, date, 'different object') }) test(label + ' – nested date in array', async ({is, isNot}) => { const date = new Date() is(+clone({d: [date]}).d[0], +date, 'same value') isNot(clone({d: [date]}).d[0], date, 'different object') is(+cloneCircles({d: [date]}).d[0], +date, 'same value') isNot(cloneCircles({d: [date]}).d, date, 'different object') }) test(label + ' – nested null', async ({is}) => { is(clone({n: null}).n, null, 'same value') }) test(label + ' – arguments', async ({isNot, same}) => { function fn (...args) { same(clone(arguments), args, 'same values') isNot(clone(arguments), arguments, 'different object') } fn(1, 2, 3) }) }