📋 Deep Clone Implementation
Category: js / utils
Difficulty: easy
Here are the most reliable ways to perform a deep clone of an object in JavaScript. ✅ Structured Clone (Modern & Robust) Supports functions, dates, maps, sets, circular references, etc. [code example] ✅ Fast and standard. ❌ Not supported in older environments (e.g., Node <17, legacy browsers). ✅ 2. Lodash cloneDeep [code example] ✅ Handles most edge cases. ✅ Works in all JS environments. ❌ Requires external dependency. ⚠️ 3. Manual Recursion (Custom Deep Clone) For full control; avoid unless necessary. [code example] ✅ Fine-grained control. ❌ Error-prone, needs frequent updates for edge cases. ⚠️ 4. JSON.parse(JSON.stringify(obj)) (Not Recommended for Complex Objects) [code example] ❌ Strips functions, undefined, symbols, dates, regex, etc. ✅ Okay for simple objects only. Recommendation: Use structuredClone if environment supports it. Else use cloneDeep from Lodash. Use manual recursion only when you need custom behavior. Let me know your runtime constraints if you want a tailored version. <!-- quiz-start --> Q1: What is a major limitation of using JSON.parse(JSON.stringify(obj)) for deep cloning? [ ] It's too slow for small objects [x] It strips functions, undefined, symbols, dates, and regex [ ] It only works in Node.js [ ] It cannot handle nested objects...