目录

@performance/bad-deep-clone-check

Avoid the following inefficient deep cloning patterns: JSON.parse(JSON.stringify(foo)) and _.cloneDeep(foo).

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@performance/bad-deep-clone-check": "warn",
  }
}

Options

"@performance/bad-deep-clone-check": [
      1,
      {
        "functions": [
          "utils.clone"
        ]
      }
    ]

Example of Correct Code

The deep copy implementation provided in this example is for reference only. You need to evaluate its suitability based on specific service requirements.

  • Note that this example implementation does not support copying functions or document object model (DOM) elements. Deep copy is generally unnecessary for functions.
  • For complex object structures, using this implementation may adversely impact performance.
  • For large-scale custom object structures, use the Structured Clone algorithm or Web Worker.
// deepClone.ts
type Cloneable = object | Array<any> | Map<any, any> | Set<any> | Date | RegExp;

export function deepClone<T extends Cloneable>(source: T, weakMap = new WeakMap()): T {
  // Process primitive types and functions.
  if (typeof source !== 'object' || source === null) {
    return source;
  }
  // Process circular references.
  if (weakMap.has(source)) {
    return weakMap.get(source);
  }
  // Process special object types.
  if (source instanceof Date) {
    return new Date(source) as T;
  }
  if (source instanceof RegExp) {
    return new RegExp(source.source, source.flags) as T;
  }
  // Process arrays.
  if (Array.isArray(source)) {
    const cloneArr: any[] = [];
    weakMap.set(source, cloneArr);
    for (const item of source) {
      cloneArr.push(deepClone(item, weakMap));
    }
    return cloneArr as T;
  }
  // Process Maps.
  if (source instanceof Map) {
    const cloneMap = new Map();
    weakMap.set(source, cloneMap);
    for (const [key, value] of source) {
      cloneMap.set(deepClone(key, weakMap), deepClone(value, weakMap));
    }
    return cloneMap as T;
  }
  // Process Sets.
  if (source instanceof Set) {
    const cloneSet = new Set();
    weakMap.set(source, cloneSet);
    for (const value of source) {
      cloneSet.add(deepClone(value, weakMap));
    }
    return cloneSet as T;
  }
  // Process common objects.
  const cloneObj = Object.create(Object.getPrototypeOf(source));
  weakMap.set(source, cloneObj);
  // Use Object.getOwnPropertyDescriptors to obtain all property descriptors.
  const descriptors = Object.getOwnPropertyDescriptors(source);

  for (const [key, descriptor] of Object.entries(descriptors)) {
    if (descriptor.value !== undefined) {
      descriptor.value = deepClone(descriptor.value, weakMap);
    }
    Object.defineProperty(cloneObj, key, descriptor);
  }
  // Process Symbol properties.
  const symbolKeys = Object.getOwnPropertySymbols(source);
  for (const key of symbolKeys) {
    cloneObj[key] = deepClone(source[key as any], weakMap);
  }
  return cloneObj;
}
// example.ts
import { deepClone } from './deepClone';

// Usage example
const obj = {
  a: 1,
  b: [2, 3],
  c: { d: 4 },
  e: new Date(),
  f: new Map([['key', 'value']]),
  g: new Set([1, 2, 3]),
  h: /regexp/gim
};
const cloned = deepClone(obj);

Example of Incorrect Code

import _ from 'lodash';
/**
 * To download the lodash dependency, run the following:
 * 1. ohpm install lodash
 * 2. ohpm install @types/lodash --save-dev
*/
interface Foo {
id: number;
name: string;
}
const foo:Foo = {
id:1,
name:"aa"
}
const clone1:Foo = JSON.parse(JSON.stringify(foo)) as Foo;
const clone2:Foo = _.cloneDeep(foo);

Rule Set

plugin:@performance/recommended
plugin:@performance/all

For details about how to configure Code Linter check rules, see Code Linter.