← Snippets

Deep Clone

 function deepClone(obj, map = new Map()) {
  if (obj === null || typeof obj !== "object") {
    return obj;
  }

  if (map.has(obj)) {
    return map.get(obj);
  }

  const copy = Array.isArray(obj) ? [] : {};

  map.set(obj, copy);

  for (const key in obj) {
    copy[key] = deepClone(obj[key], map);
  }

  return copy;
}