Class 4 __node buffer - Alwynblake/seattle-javascript-401n12 GitHub Wiki
Prior to the introduction of TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts.
Instances of the Buffer class are similar to arrays of integers but correspond to fixed-sized, raw memory allocations outside the V8 heap. The size of the Buffer is established when it is created and cannot be changed.
The Buffer class is within the global scope, making it unlikely that one would need to ever use require('buffer').Buffer.
// Creates a zero-filled Buffer of length 10. const buf1 = Buffer.alloc(10);
// Creates a Buffer of length 10, filled with 0x1. const buf2 = Buffer.alloc(10, 1);
// Creates an uninitialized buffer of length 10. // This is faster than calling Buffer.alloc() but the returned // Buffer instance might contain old data that needs to be // overwritten using either fill() or write(). const buf3 = Buffer.allocUnsafe(10);
// Creates a Buffer containing [0x1, 0x2, 0x3]. const buf4 = Buffer.from([1, 2, 3]);
// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74]. const buf5 = Buffer.from('tést');
// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74]. const buf6 = Buffer.from('tést', 'latin1');