structuredClone vs arrayBuffer

Benchmark created on


Setup

function generateLargeObject(depth = 3, breadth = 5, arraySize = 10) {
    function createNestedObject(level) {
        if (level === 0) {
            return {
                data: Array.from({ length: arraySize }, (_, i) => `Item ${i}`),
                value: Math.random(),
            };
        }

        const obj = {};
        for (let i = 0; i < breadth; i++) {
            obj[`key${level}_${i}`] = createNestedObject(level - 1);
        }
        return obj;
    }

    return createNestedObject(depth);
}

const obj = generateLargeObject(7, 7, 20);


Test runner

Ready to run.

Testing in
TestOps/sec
Structuredclone
window.structuredClone(obj);
ready
arrayBuffer
const convertObjectToArrayBuffer = obj => {
  try {
    const jsonString = JSON.stringify(obj);

    // Convert to ArrayBuffer
    const encoder = new TextEncoder();
    const arrayBuffer = encoder.encode(jsonString).buffer;
    return arrayBuffer;
  } catch (error) {
    console.error("Failed to convert object to ArrayBuffer:", error);
    throw error;
  }
};

convertObjectToArrayBuffer(obj);
ready

Revisions

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