nv-facutil-copy

deep-copy,deep-clone,Circular

Usage no npm install needed!

<script type="module">
  import nvFacutilCopy from 'https://cdn.skypack.dev/nv-facutil-copy';
</script>

README

nv-facutil-copy

  • nv-facutil-copy is a very simple util to let you deep-copy/deep-clone a object
  • include [Circular] ,undefined, Map,Set,Dict,Array
  • can only used in nodejs, NOT work in browser,coz it use require("v8")

install

  • npm install nv-facutil-copy

usage

example

const {dcp,parse,stringify} = require("nv-facutil-copy");

deep-copy circular

var mp = new Map()

var d = {}
d['a'] = d
d.b = undefined

var arr = [undefined,1,2,3]
arr.push(arr)

var j = {arr,d}

var st = new Set()
st.add(st)

mp.set(d,st)
mp.set(arr,j)
mp.set("self",mp)

/*
Map {
  { a: [Circular], b: undefined } => Set { [Circular] },
  [ undefined, 1, 2, 3, [Circular] ] => {
    arr: [ undefined, 1, 2, 3, [Circular] ],
    d: { a: [Circular], b: undefined }
  },
  'self' => [Circular]
}
>

*/

var nobj = dcp(mp)
nobj !== mp
var ndict = Array.from(nobj.keys())[0]

> ndict
{ a: [Circular], b: undefined }
> ndict.a === ndict
true
> d !== ndict
true
> d.b === ndict.b
true
>


/*
> Array.from(nobj.keys())
[
  { a: [Circular], b: undefined },
  [ undefined, 1, 2, 3, [Circular] ],
  'self'
]
>


> nobj
Map {
  { a: [Circular], b: undefined } => Set { [Circular] },
  [ undefined, 1, 2, 3, [Circular] ] => {
    arr: [ undefined, 1, 2, 3, [Circular] ],
    d: { a: [Circular], b: undefined }
  },
  'self' => [Circular]
}
>

*/

var json = {1:[1,2,3],2:{'a':undefined}}

/*
> dcp(json)
{ '1': [ 1, 2, 3 ], '2': { a: undefined } }
>

*/

dump

> var s = stringify(mp)
> s
`ΓΏ\r;o"\u0001a^\u0001"\u0001b_{\u0002'^\u0002,\u0001A\u0005_I\u0002I\u0004I\u0006^\u0003$\u0000\u0005o"\u0003arr^\u0003"\u0001d^\u0001{\u0002"\u0004self^\u0000:\u0006`
>

load

> var nobj = parse(s)
> nobj
Map {
  { a: [Circular], b: undefined } => Set { [Circular] },
  [ undefined, 1, 2, 3, [Circular] ] => {
    arr: [ undefined, 1, 2, 3, [Circular] ],
    d: { a: [Circular], b: undefined }
  },
  'self' => [Circular]
}
>

API

  • fac_cp.dcp(obj);
  • fac_cp.parse(dumped_string);
  • fac_cp.stringify(obj);

RESTRICT

  • only work in nodejs
  • its slow, because it used v8.deserialize
  • NOT suitable for frequently calling in a function

performance

    function orig_dcp(j) {
        return(JSON.parse(JSON.stringify(j)))
    }

    function slow_dcp(j) {return(dcp(j))}

    var json = {1:[1,2,3],2:{'a':undefined}}


    > orig_dcp(json)
    { '1': [ 1, 2, 3 ], '2': {} }
    >
    undefined
    > slow_dcp(json)
    { '1': [ 1, 2, 3 ], '2': { a: undefined } }
    >

    function tst(times,f,...args) {
        let start = perf_hooks.performance.nodeTiming.duration
        c= 0
        while(c<times) {f(...args);c=c+1}
        let end = perf_hooks.performance.nodeTiming.duration
        console.log(end-start)
    }

    > tst(50000,orig_dcp,json)
    151.65841102600098
    undefined
    > tst(50000,slow_dcp,json)
    2831.4293529987335
    undefined
    >

LICENSE

  • ISC