Buffer and es6 Binary Arrays of Nodejs

  node.js, question

May I ask the relation between buffer and es6 binary array in node?

Before ES6, JS could not directly process binary data. Node added Buffer to make up for this deficiency.

ES6 has added some classes that deal with binary data. These classes have the same functions as Buffer, but also have some differences. Let’s briefly talk about the differences.


ES6 is more cautious in handling binary data and provides many related classes. ArrayBuffer is used to represent the original binary data, but you cannot directly manipulate the binary data it represents. You can use TypedArray to access binary data as an array of some basic data type, or use DataView to read and write and modify binary data.

TypedArray is only a concept, not a directly usable class. TypedArray refers to the following classes.

  • Int8Array

  • Uint8Array

  • Uint8ClampedArray

  • Int16Array

  • Uint16Array

  • Int32Array

  • Uint32Array

  • Float32Array

  • Float64Array

As shown by the name, the corresponding binary data are respectively expressed in the corresponding data format, and there is no conversion.

DataViewIs used to read and write binary data, you can create from the existing binary data, in order to operate the binary data. The key is that if you need to specify a large end or a small segment, you need to pass it in as a parameter. For exampledataview.getInt32(byteOffset [, littleEndian])


There is only one Buffer class used to represent binary data, on which you can read and write data and access data. This should be needless to say, more or less certainly used.



In fact, I think Buffer is convenient. DataView must be created on ArrayBuffer or TypedArray, so it is a bit troublesome to use, but the reading and writing operations are similar.

Source code

ArrayBufferThose classes, byv8Implementation, andBufferIn fact, it is also usedv8TheArrayBufferTo package.

//node.js6.3.1 src/node _ buffer.cc309 line
 MaybeLocal<Object> New(Environment* env, size_t length) {
 EscapableHandleScope scope(env->isolate());
 // V8 currently only allows a maximum Typed Array index of max Smi.
 if (length > kMaxLength) {
 return Local<Object>();
 void* data;
 if (length > 0) {
 data = BUFFER_MALLOC(length);  //Allocate Memory Directly with malloc
 if (data == nullptr)
 return Local<Object>();
 } else {
 data = nullptr;
 Local<ArrayBuffer> ab = // encapsulates memory into ArrayBuffer
 Local<Uint8Array> ui = Uint8Array::New(ab, 0, length);  // Buffer starts with Uint8Array.
 Maybe<bool> mb =
 ui->SetPrototype(env->context(), env->buffer_prototype_object());
 if (mb.FromMaybe(false))
 return scope.Escape(ui);
 // Object failed to be created. Clean up resources.
 return Local<Object>();

Therefore, Buffer can basically be considered as a package for ES6 to process binary data. You don’t need to write Uint8Array, DataView and other classes to process data, you can just directly operate on Buffer. Moreover, this C/C plus package is much better than JS itself.

The problem is that ES6 is more powerful, and even there is a SharedArrayBuffer, which is allocated on the shared memory. If you are interested, you can check the data.


After midnight so many words, give some approval.