deep cloning a typed array in Javascript

479 Views Asked by At

Trying to deep clone an array buffer (i.e, pass by value not by reference)

const deepClone = (buf) => {
    uint32 = new Uint32Array(buf);
    let newBuf = new ArrayBuffer(buf.byteLength);
    let uint32new = new Uint32Array(newBuf);

    //  This works fine
    uint32.forEach((el, i) => {
        uint32new[i] = el;
    });

    //  but this doesn't
    uint32new = uint32.map((el) => {return el});

    //  neither does this
    uint32new = [...uint32];

    return newBuf;
};  

The latter 2 merely return an empty (0's) buffer

why?

0

There are 0 best solutions below