structuredClone(x) vs JSON.parse(JSON.stringify(x)) (v2)

Revision 2 of this benchmark created on


Description

3 pairs of tests. Just focus on the ops/sec between each pair.

Setup

const count = 10_000;
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.,_:;#+*'?=)(/&%$!";

function randomString(size) {
	var string = "";
	for ( let i = 0; i < size; i++ ) {
		string += chars[Math.floor(Math.random() * chars.length)];
	}
	
	return string;
}

var hugeArrayOfObjects = [];
for ( let i = 0; i < count; i++ ) {
	hugeArrayOfObjects.push({
		lon: Math.random() * 360 - 180,
		lat: Math.random() * 180 - 90,
		a: randomString(32),
		b: randomString(16),
		c: randomString(48),
	});
}

var hugeArrayOfStrings = [];
for ( let i = 0; i < count; i++ ) {
	const src = hugeArrayOfObjects[i];

	hugeArrayOfStrings.push([src.lon, src.lat, src.a, src.b, src.c].join(";"));
}

var hugeString = hugeArrayOfStrings.join("|");

Test runner

Ready to run.

Testing in
TestOps/sec
json hugeArrayOfStrings
const a = JSON.parse(JSON.stringify(hugeArrayOfStrings));
ready
structuredClone hugeArrayOfStrings
const b = structuredClone(hugeArrayOfStrings);
ready
json hugeArrayOfObjects
const c = JSON.parse(JSON.stringify(hugeArrayOfObjects));
ready
structuredClone hugeArrayOfObjects
const d = structuredClone(hugeArrayOfObjects);
ready
json string
const e = JSON.parse(JSON.stringify(hugeString));
ready
structuredClone string
const f = structuredClone(hugeString);
ready

Revisions

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