← Back to Snippets
JavaScriptMay 2026

Deep clone without JSON.parse

Clones an object deeply without losing Dates, undefined values, or circular references.

snippet
// Modern: structuredClone (Node 17+, all modern browsers)
const clone = structuredClone(original)
// Handles: Date, RegExp, Map, Set, ArrayBuffer, circular refs
// Does NOT handle: functions, class instances, symbols

// Legacy fallback with circular reference support
function deepClone(obj, seen = new WeakMap()) {
  if (obj === null || typeof obj !== 'object') return obj
  if (seen.has(obj)) return seen.get(obj)
  const copy = Array.isArray(obj) ? [] : {}
  seen.set(obj, copy)
  for (const key of Object.keys(obj)) {
    copy[key] = deepClone(obj[key], seen)
  }
  return copy
}
ObjectsUtilitystructuredClone