Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs buffers vs typed arrays

Tags:

What is more efficient - nodejs buffers or typed arrays? What should I use for better performance? I think that only those who know interiors of V8 and NodeJs could answer this question.

like image 475
Andrey Kon Avatar asked Apr 03 '12 06:04

Andrey Kon


People also ask

What is the difference between buffer and array?

Generally speaking: Buffers store an array of unformatted memory. An array is a general term of contiguous memory. A buffer needs to be bound to the context, whereas an array is just an array of data. If i recall correctly, the data in the buffer is meant to be copied onto the graphics card (hence the binding).

Why might you use typed arrays instead of standard arrays?

A typed array significantly simplifies the level of proof the engine needs to be able to optimise around it. A value returned from a typed array is certainly of a certain type, and engines can optimise for the result being that type.

Are typed arrays faster?

The array does not contain holes (yeah, holes are fast but…). The array is preallocated. So creating and initialization typed array is fast.

Why do we need buffers in Node JS?

Buffers in Node. js is used to perform operations on raw binary data. Generally, Buffer refers to the particular memory location in memory. Buffer and array have some similarities, but the difference is array can be any type, and it can be resizable. Buffers only deal with binary data, and it can not be resizable.


2 Answers

A Node.js buffer should be more efficient than a typed array. The reason is simply because when a new Node.js Buffer is created it does not need to be initialized to all 0's. Whereas, the HTML5 spec states that initialization of typed arrays must have their values set to 0. Allocating the memory and then setting all of the memory to 0's takes more time.

In most applications picking either one won't matter. As always, the devil lies in the benchmarks :) However, I recommend that you pick one and stick with it. If you're often converting back and forth between the two, you'll take a performance hit.

Nice discussion here: https://github.com/joyent/node/issues/4884

like image 158
JP Richardson Avatar answered Sep 20 '22 16:09

JP Richardson


There are a few things that I think are worth mentioning:

  1. Buffer instances are Uint8Array instances but there are subtle incompatibilities with the TypedArray specification in ECMAScript 2015. For example, while ArrayBuffer#slice() creates a copy of the slice, the implementation of Buffer#slice() creates a view over the existing Buffer without copying, making Buffer#slice() far more efficient.
  2. When using Buffer.allocUnsafe() and Buffer.allocUnsafeSlow() the memory isn't zeroed-out (as many have pointed out already). So make sure you completely overwrite the allocated memory or you can allow the old data to be leaked when the Buffer memory is read.
  3. TypedArrays are not readable right away, you'll need a DataView for that. Which means you might need to rewrite your code if you were to migrate back to Buffer. Adapter pattern could help here.
  4. You can use for-of on Buffer. You cannot on TypedArrays. Also you won't have the classic entries(), values(), keys() and length support.
  5. Buffer is not supported in the frontend while TypedArray may well be. So if your code is shared between frontend or backend you might consider sticking to one.

More info in the docs here.

like image 39
Francesco Casula Avatar answered Sep 19 '22 16:09

Francesco Casula