Clone Object
Creates a deep clone of an object using structuredClone or deterministic JSON serialization.
cloneObject
The cloneObject function creates a deep clone of an object. By default, it uses the browser/runtime's built-in structuredClone API. It can also force deterministic JSON serialization.
Function Signature
function cloneObject<T extends GenericObject>(obj: T, serialize?: boolean): TParameters
obj(T): The object to clone.serialize(boolean, optional): Whentrue, forces deterministic JSON serialization with sorted keys. Defaults tofalse.
Return Value
T: A new object that is a deep clone of the input.
Example Usage
playground.ts
Behavior Details
- Default Mode (
serialize = false): UsesstructuredCloneif available. This preserves circular references,Dateobjects,Map,Set,RegExp, and Typed Arrays. - Serialization Mode (
serialize = true): UsesstableStringifyinternally. All object keys are sorted alphabetically.undefinedvalues are converted tonull. - Safety Fallback: If both methods fail (e.g. circular references in JSON mode), it returns a shallow clone (
{ ...obj }) instead of throwing an error.
Types
GenericObject
type GenericObject = Record<string, any>;Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
Object Utilities
Essential utilities for object manipulation, deep/shallow cloning, flattening, merging, and type-safe transformation in TypeScript/JavaScript.
Sanitize Data
Cleans and normalizes strings, arrays, and objects by trimming whitespace, removing empty values, and applying customizable filters.
