Javascript ArrayBuffer modification performance

Viewed 1084

I want to modify an ArrayBuffer's contents in JavaScript.

From the help section:

You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.

I don't need to print anything to console, I just need an ArrayBuffer with some bytes modified.

So, if I have a large ArrayBuffer:

const buffer = new ArrayBuffer(16*1024);

Which one is more effective:

const typedArray1 = new Uint8Array(buffer);
typedArray1[16000] = 65;

const typedArray2 = new Uint8Array(buffer,16000);
typedArray2[0] = 65;

const typedArray3 = new Uint8Array(buffer,16000,1);
typedArray2[0] = 65;

const dataView1 = new DataView(buffer);
dataView1.setUint8(16000, 65);

const dataView2 = new DataView(buffer, 16000);
dataView2.setUint8(0, 65);

const dataView3 = new DataView(buffer, 16000, 1);
dataView3.setUint8(0, 65);
0 Answers
Related