5

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);
Daniel
  • 2,318
  • 2
  • 22
  • 53
  • The TypedArray value settings operations are more straightforward than DataView. I'd imagine you'd create some overhead with the sliced versions, but nothing more will be allocated in memory in any of these cases, so if it's a one shot, you won't be able to measure any meaningful difference in any of these. So the real question is, "do you experience a real bottleneck where this decision may have an impact?". Remember a TypedArray view costs nothing. – Kaiido Mar 11 '20 at 07:06
  • So you are saying that `typedArray1` is the best way to go? – Daniel Mar 11 '20 at 09:58
  • I'm saying that it won't matter in this case. But yes, generally speaking it should theoretically be the "most efficient" one. – Kaiido Mar 11 '20 at 10:00
  • 2
    https://v8.dev/blog/dataview - here they show that currently (2018) dataview was **4 times** slower than typedarray. Just found this. Anyway the close vote "needs details or clarity" seems unfair. :) – Daniel Mar 11 '20 at 10:00
  • Nope it's not "unfair". As I said in my first comment, without a clear case, asking which is faster makes no sense. They will all 6 literally take no time. Whatever differences you could see would be due to other overheads. If your case requires a DataView (e.g you want to set different value types at different indexes), then a DataView is more appropriate. Without seeing an exact case, we can only give non-sense: your question needs more details and clarity. – Kaiido Mar 11 '20 at 10:08
  • 1
    Aha. "I just need an ArrayBuffer with some bytes modified" - that is the case, which I believe describes the use case: I don't need either DataView, nor TypedArray. It's just a must based on ArrayBuffer's spec and I wanted to know which one is better in terms of performance. There are always differences in performance and usually community is very good in these questions. You've said all 6 are very similar, but I found a site which states huge differences in between - that's why it feels unfair. Even though these diffs might already have disappeared. – Daniel Mar 11 '20 at 10:17
  • I said there is no difference for a single operation. I also exactly said where you could theoretically see drawbacks, but with a single operation, you won't see it. – Kaiido Mar 11 '20 at 10:18

0 Answers0