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.
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).
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.
The array does not contain holes (yeah, holes are fast but…). The array is preallocated. So creating and initialization typed array is fast.
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.
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
There are a few things that I think are worth mentioning:
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.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.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. Buffer
. You cannot on TypedArrays
. Also you won't have the classic entries()
, values()
, keys()
and length
support.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With