This post will discuss how to efficiently perform a deep copy of an object in JavaScript and jQuery.

Deep copying an object in JavaScript means creating a new object that has the same properties and values as the original object, but is independent from it. This means that modifying the new object will not affect the original object, and vice versa. An object in JavaScript can contain circular references, functions, Array, Map, Set, Blob, Date, RegExp, and several other complex types, it is not recommended writing our own deep clone function. However, there are several third-party libraries that offers deep cloning functionality:

1. Using Lodash Library

The Lodash library has the _.cloneDeep function that can recursively clone the value of an object, including any nested objects, arrays, functions, dates, etc. It also preserves the prototype chain and the constructor of the original object. Following is a simple example demonstrating usage of this function:

Download Code

Output:

{
  string: ‘primitive string’,
  stringObj: [String: ‘string object’],
  number: 100,
  numberObj: [Number: 100],
  bigInt: 9007199254740991n,
  nan: NaN,
  array: [ 1, 2, 3 ],
  arrayObj: [ undefined, undefined ],
  bool: false,
  boolobj: [Boolean: false],
  nul: null,
  date: 2020-10-02T11:21:37.553Z,
  undef: undefined,
  inf: Infinity,
  regExp: /(\w+)\s(\w+)/
}

2. Using jQuery

Alternatively, we can deep clone an object in JavaScript using jQuery Library. We can use the jQuery’s $.extend() function, which copies all the properties and values from one or more source objects to a target object. We can pass a boolean value as the first argument to indicate whether we want a deep or shallow copy. For example, when the first argument to $.extend is true, the merge becomes recursive and performs a deep copy.

Download Code

Output:

{
  string: ‘primitive string’,
  stringObj: [String: ‘string object’],
  number: 100,
  numberObj: [Number: 100],
  bigInt: 9007199254740991n,
  nan: NaN,
  array: [ 1, 2, 3 ],
  arrayObj: [],
  bool: false,
  boolobj: [Boolean: false],
  nul: null,
  date: 2020-10-02T11:22:42.848Z,
  inf: Infinity,
  regExp: /(\w+)\s(\w+)/
}

3. Using custom function

This involves writing our own function that recursively copies all the properties and values from the original object to a new object, taking care of nested objects, arrays, and other complex types. This way, we get a new object that is identical to the original one in every aspect, but has a different reference. For example, the following code can be used as a starting point for all our copying needs:

Download  Run Code

Output:

{
  string: ‘primitive string’,
  stringObj: [String: ‘string object’],
  number: 100,
  numberObj: [Number: 100],
  bigInt: 9007199254740991n,
  nan: NaN,
  array: [ 1, 2, 3 ],
  arrayObj: [ <2 empty items> ],
  bool: false,
  boolobj: [Boolean: true],
  nul: null,
  date: 2020-10-02T11:23:21.746Z,
  undef: undefined,
  inf: Infinity,
  regExp: /(\w+)\s(\w+)/
}

 
This function is the most flexible and reliable, but it is also the most complex and time-consuming. It requires us to handle different types of objects and values, and to check for circular references or infinite recursion. It also may not work for some objects that have non-enumerable or non-writable properties, or that use getters and setters.

That’s all about performing a deep copy of an object in JavaScript and jQuery.