Buffer
Many methods in Node.js require the use of a Buffer
. This type allows data to be copied in and out quickly—no allocations are needed and code runs faster.
On the Buffer
, we can simplify searching with methods like indexOf
. When processing a file in a stream, using indexOf
allows fast analysis of files, with no copying.
Even though in most programs we do not need to manually create a Buffer
, it is useful to know how it can be done. We can call alloc()
or from for initialization.
Buffer.alloc
method with a single integer argument—the length of the buffer. The buffer is initialized to all zeros.byte
can be used for initialization. Here the space string
is specified, which has the hex value 20 in ASCII.Buffer.from()
to convert an Array to a Buffer
. Typed arrays, and even strings can also be converted.Uint8Array
, which is a typed array that is stored efficiently in memory, and copy it to a buffer.const { Buffer } = require("node:buffer"); // Version 1: call alloc with integer argument. const buf = Buffer.alloc(10); console.log(buf); // Version 2: fill the buffer with a default byte. const buf2 = Buffer.alloc(10, " "); console.log(buf2); // Version 3: create a buffer from an array of values. const values = [10, 20, 30, 40, 50]; const buf3 = Buffer.from(values); console.log(buf3); // Version 4: create a buffer from a Uint8 array. const values2 = new Uint8Array([1, 2, 3, 4]); const buf4 = Buffer.from(values2); console.log(buf4);<Buffer 00 00 00 00 00 00 00 00 00 00> <Buffer 20 20 20 20 20 20 20 20 20 20> <Buffer 0a 14 1e 28 32> <Buffer 01 02 03 04>
IndexOf
Usually it is necessary to perform some sort of action upon a Buffer
. One thing we can do is fast searches with indexOf
, much like the same-named string
method.
Buffer
with the data from a string
. For clarity, we then call toString()
to display the Buffer
's data.string
"dog" does not occur in the Buffer
's data, so indexOf
returns -1 here.string
is returned (which is the value 4).lastIndexOf
searches from the last part of the string
forwards, so it returns the value 20 for "cat."indexOf
, we often need to get all matches. A while loop that repeatedly calls indexOf
with a start index can be used.const { Buffer } = require("node:buffer"); // Part 1: create buffer from a string. const buf = Buffer.from("the cat, the orange cat"); console.log(buf.toString()); // Part 2: if a value is not found, indexOf returns -1. console.log(buf.indexOf("dog")) // Part 3: the first cat is found. console.log(buf.indexOf("cat")); // Part 4: the last cat is found. console.log(buf.lastIndexOf("cat")); // Part 5: run an indexOf loop that finds all instances of a pattern in a Buffer. var position = 0; while (true) { const index = buf.indexOf("cat", position); if (index == -1) { break; } console.log("Cat at", index); position += index + 1; }the cat, the orange cat -1 4 20 Cat at 4 Cat at 20
In Node, Buffers are usually used as part of a stream, not on their own. But understanding the core concepts of this type becomes helpful when implementing advanced stream processing.