cloning objects. structuredClone(x) vs JSON.parse(JSON.stringify(x))

Benchmark created on


Description

If speed is your priority and your objects contain only simple data, go with JSON.parse(JSON.stringify(obj)). If your object has complex types like Date, Function, BigInt, Map, or Set, or if you want better compatibility, use structuredClone(obj).

Setup

const obj = {
	name: 'test',
	email: 'email@example.com',
	phone: '+123456789',
};

Test runner

Ready to run.

Testing in
TestOps/sec
structuredClone
const a = structuredClone(obj);
ready
JSON.parse + JSON.stringify
const b = JSON.parse(JSON.stringify(obj));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.