10LOC

#javascript-fundamentals

Node.jsintermediate

structuredClone for a true deep copy, no JSON hacks

type Session = { id: string; startedAt: Date; roles: Set<string>; meta: Map<string, number> };

export const cloneSession = (session: Session): Session => structuredClone(session);

const original: Session = {

structuredClone() deep-copies Dates, Maps, Sets, and circular references correctly — JSON.parse(JSON.stringify()) silently mangles all four.