Home
Map
Buffer ExamplesCreate Buffers from arrays and strings, and use methods like indexOf to search buffer data.
Node.js
This page was last reviewed on Dec 20, 2023.
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.
fs createReadStream
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.
Initialize example. 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.
Version 1 We call the Buffer.alloc method with a single integer argument—this is the length of the buffer. The buffer is initialized to all zeros.
Version 2 A default byte can be used for initialization. Here the space string is specified, which has the hex value 20 in ASCII.
Version 3 We can use Buffer.from() to convert an Array to a Buffer. Typed arrays, and even strings can also be converted.
Array
Version 4 We create a Uint8Array, which is a typed array that is stored efficiently in memory, and copy it to a buffer.
Int32Array
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.
Part 1 We create a Buffer with the data from a string. For clarity, we then call toString() to display the Buffer's data.
Part 2 The string "dog" does not occur in the Buffer's data, so indexOf returns -1 here.
Part 3 When we search for "cat," the first index of the string is returned (which is the value 4).
Part 4 Meanwhile, lastIndexOf searches from the last part of the string forwards, so it returns the value 20 for "cat."
Part 5 When using 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
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 20, 2023 (new).
Home
Changes
© 2007-2024 Sam Allen.