Cloning: what's the fastest alternative to JSON.parse(JSON.stringify(x))?

前端 未结 3 1993
青春惊慌失措
青春惊慌失措 2021-01-30 13:21

What\'s the fastest alternative to

JSON.parse(JSON.stringify(x))

There must be a nicer/built-in way to perform a deep clone on objects/arrays,

3条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 14:08

    I was actually comparing it against angular.copy

    You can run the JSperf test here: https://jsperf.com/angular-copy-vs-json-parse-string

    I'm comparing:

    myCopy = angular.copy(MyObject);
    

    vs

    myCopy = JSON.parse(JSON.stringify(MyObject));
    

    This is the fatest of all test I could run on all my computers

提交回复
热议问题