Question: What is the most efficient way to clone a JavaScript object? I've seen `obj = eval(uneval(o));` being used, but [that's non-standard and only supported by Firefox](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/uneval).  

 I've done things like `obj = JSON.parse(JSON.stringify(o));` but question the efficiency.   

 I've also seen recursive copying functions with various flaws.

I'm surprised no canonical solution exists.

Answer: 
**Deep copy by performance:**
Ranked from best to worst

* spread operator `...` (primitive arrays - only)
* `splice(0)` (primitive arrays - only)
* `slice()` (primitive arrays - only)
* `concat()` (primitive arrays - only)
* custom function, as seen below (any array)
* jQuery's `$.extend()` (any array)
* `JSON.parse(JSON.stringify())` (primitive and literal arrays - only)
* Underscore's `_.clone()` (primitive and literal arrays - only)
* Lodash's `_.cloneDeep()` (any array)

Where:

* primitives = strings, numbers, and booleans
* literals = object literals `{}`, array literals `[]`
* any = primitives, literals, and prototypes

**Deep copy an array of primitives:**

```js
let arr1a = [1, 'a', true];

```

To deep copy arrays with primitives only (i.e. numbers, strings, and booleans), reassignment, `slice()`, `concat()`, and Underscore's `clone()` can be used.

Where spread has the fastest performance:

```js
let arr1b = [...arr1a];

```

And where `slice()` has better performance than `concat()`: <https://jsbench.me/x5ktn7o94d/>

```js
let arr1c = arr1a.splice(0);
let arr1d = arr1a.slice();
let arr1e = arr1a.concat();

```

**Deep copy an array of primitive and object literals:**

```js
let arr2a = [1, 'a', true, {}, []];
let arr2b = JSON.parse(JSON.stringify(arr2a));

```

**Deep copy an array of primitive, object literals, and prototypes:**

```js
let arr3a = [1, 'a', true, {}, [], new Object()];

```

Write a custom function (has faster performance than `$.extend()` or `JSON.parse`):

```js
function copy(aObject) {
  // Prevent undefined objects
  // if (!aObject) return aObject;

  let bObject = Array.isArray(aObject) ? [] : {};

  let value;
  for (const key in aObject) {

    // Prevent self-references to parent object
    // if (Object.is(aObject[key], aObject)) continue;

    value = aObject[key];

    bObject[key] = (typeof value === "object") ? copy(value) : value;
  }

  return bObject;
}

let arr3b = copy(arr3a);

```

Or use third-party utility functions:

```js
let arr3c = $.extend(true, [], arr3a); // jQuery Extend
let arr3d = _.cloneDeep(arr3a); // Lodash

```

Note: jQuery's `$.extend` also has better performance than `JSON.parse(JSON.stringify())`:

* [js-deep-copy](https://jsbench.me/5kktn8f3ss)
* [jquery-extend-vs-json-parse](https://jsbench.me/imktn8ppco)
The following creates two instances of the same object. I found it and am using it currently. It's simple and easy to use.

```
var objToCreate = JSON.parse(JSON.stringify(cloneThis));

```