• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Buffer
2
3<!--introduced_in=v0.1.90-->
4
5> Stability: 2 - Stable
6
7<!-- source_link=lib/buffer.js -->
8
9`Buffer` objects are used to represent a fixed-length sequence of bytes. Many
10Node.js APIs support `Buffer`s.
11
12The `Buffer` class is a subclass of JavaScript's [`Uint8Array`][] class and
13extends it with methods that cover additional use cases. Node.js APIs accept
14plain [`Uint8Array`][]s wherever `Buffer`s are supported as well.
15
16The `Buffer` class is within the global scope, making it unlikely that one
17would need to ever use `require('buffer').Buffer`.
18
19```js
20// Creates a zero-filled Buffer of length 10.
21const buf1 = Buffer.alloc(10);
22
23// Creates a Buffer of length 10,
24// filled with bytes which all have the value `1`.
25const buf2 = Buffer.alloc(10, 1);
26
27// Creates an uninitialized buffer of length 10.
28// This is faster than calling Buffer.alloc() but the returned
29// Buffer instance might contain old data that needs to be
30// overwritten using fill(), write(), or other functions that fill the Buffer's
31// contents.
32const buf3 = Buffer.allocUnsafe(10);
33
34// Creates a Buffer containing the bytes [1, 2, 3].
35const buf4 = Buffer.from([1, 2, 3]);
36
37// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries
38// are all truncated using `(value & 255)` to fit into the range 0–255.
39const buf5 = Buffer.from([257, 257.5, -255, '1']);
40
41// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':
42// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)
43// [116, 195, 169, 115, 116] (in decimal notation)
44const buf6 = Buffer.from('tést');
45
46// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].
47const buf7 = Buffer.from('tést', 'latin1');
48```
49
50## Buffers and character encodings
51<!-- YAML
52changes:
53  - version: v6.4.0
54    pr-url: https://github.com/nodejs/node/pull/7111
55    description: Introduced `latin1` as an alias for `binary`.
56  - version: v5.0.0
57    pr-url: https://github.com/nodejs/node/pull/2859
58    description: Removed the deprecated `raw` and `raws` encodings.
59-->
60
61When converting between `Buffer`s and strings, a character encoding may be
62specified. If no character encoding is specified, UTF-8 will be used as the
63default.
64
65```js
66const buf = Buffer.from('hello world', 'utf8');
67
68console.log(buf.toString('hex'));
69// Prints: 68656c6c6f20776f726c64
70console.log(buf.toString('base64'));
71// Prints: aGVsbG8gd29ybGQ=
72
73console.log(Buffer.from('fhqwhgads', 'utf8'));
74// Prints: <Buffer 66 68 71 77 68 67 61 64 73>
75console.log(Buffer.from('fhqwhgads', 'utf16le'));
76// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>
77```
78
79The character encodings currently supported by Node.js are the following:
80
81* `'utf8'`: Multi-byte encoded Unicode characters. Many web pages and other
82  document formats use [UTF-8][]. This is the default character encoding.
83  When decoding a `Buffer` into a string that does not exclusively contain
84  valid UTF-8 data, the Unicode replacement character `U+FFFD` � will be used
85  to represent those errors.
86
87* `'utf16le'`: Multi-byte encoded Unicode characters. Unlike `'utf8'`, each
88  character in the string will be encoded using either 2 or 4 bytes.
89  Node.js only supports the [little-endian][endianness] variant of [UTF-16][].
90
91* `'latin1'`: Latin-1 stands for [ISO-8859-1][]. This character encoding only
92  supports the Unicode characters from `U+0000` to `U+00FF`. Each character is
93  encoded using a single byte. Characters that do not fit into that range are
94  truncated and will be mapped to characters in that range.
95
96Converting a `Buffer` into a string using one of the above is referred to as
97decoding, and converting a string into a `Buffer` is referred to as encoding.
98
99Node.js also supports the following two binary-to-text encodings. For
100binary-to-text encodings, the naming convention is reversed: Converting a
101`Buffer` into a string is typically referred to as encoding, and converting a
102string into a `Buffer` as decoding.
103
104* `'base64'`: [Base64][] encoding. When creating a `Buffer` from a string,
105  this encoding will also correctly accept "URL and Filename Safe Alphabet" as
106  specified in [RFC 4648, Section 5][]. Whitespace characters such as spaces,
107  tabs, and new lines contained within the base64-encoded string are ignored.
108
109* `'hex'`: Encode each byte as two hexadecimal characters. Data truncation
110  may occur when decoding strings that do exclusively contain valid hexadecimal
111  characters. See below for an example.
112
113The following legacy character encodings are also supported:
114
115* `'ascii'`: For 7-bit [ASCII][] data only. When encoding a string into a
116  `Buffer`, this is equivalent to using `'latin1'`. When decoding a `Buffer`
117  into a string, using this encoding will additionally unset the highest bit of
118  each byte before decoding as `'latin1'`.
119  Generally, there should be no reason to use this encoding, as `'utf8'`
120  (or, if the data is known to always be ASCII-only, `'latin1'`) will be a
121  better choice when encoding or decoding ASCII-only text. It is only provided
122  for legacy compatibility.
123
124* `'binary'`: Alias for `'latin1'`. See [binary strings][] for more background
125  on this topic. The name of this encoding can be very misleading, as all of the
126  encodings listed here convert between strings and binary data. For converting
127  between strings and `Buffer`s, typically `'utf-8'` is the right choice.
128
129* `'ucs2'`: Alias of `'utf16le'`. UCS-2 used to refer to a variant of UTF-16
130  that did not support characters that had code points larger than U+FFFF.
131  In Node.js, these code points are always supported.
132
133```js
134Buffer.from('1ag', 'hex');
135// Prints <Buffer 1a>, data truncated when first non-hexadecimal value
136// ('g') encountered.
137
138Buffer.from('1a7g', 'hex');
139// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').
140
141Buffer.from('1634', 'hex');
142// Prints <Buffer 16 34>, all data represented.
143```
144
145Modern Web browsers follow the [WHATWG Encoding Standard][] which aliases
146both `'latin1'` and `'ISO-8859-1'` to `'win-1252'`. This means that while doing
147something like `http.get()`, if the returned charset is one of those listed in
148the WHATWG specification it is possible that the server actually returned
149`'win-1252'`-encoded data, and using `'latin1'` encoding may incorrectly decode
150the characters.
151
152## Buffers and TypedArrays
153<!-- YAML
154changes:
155  - version: v3.0.0
156    pr-url: https://github.com/nodejs/node/pull/2002
157    description: The `Buffer`s class now inherits from `Uint8Array`.
158-->
159
160`Buffer` instances are also JavaScript [`Uint8Array`][] and [`TypedArray`][]
161instances. All [`TypedArray`][] methods are available on `Buffer`s. There are,
162however, subtle incompatibilities between the `Buffer` API and the
163[`TypedArray`][] API.
164
165In particular:
166
167* While [`TypedArray#slice()`][] creates a copy of part of the `TypedArray`,
168  [`Buffer#slice()`][`buf.slice()`] creates a view over the existing `Buffer`
169  without copying. This behavior can be surprising, and only exists for legacy
170  compatibility. [`TypedArray#subarray()`][] can be used to achieve the behavior
171  of [`Buffer#slice()`][`buf.slice()`] on both `Buffer`s and other
172  `TypedArray`s.
173* [`buf.toString()`][] is incompatible with its `TypedArray` equivalent.
174* A number of methods, e.g. [`buf.indexOf()`][], support additional arguments.
175
176There are two ways to create new [`TypedArray`][] instances from a `Buffer`:
177
178* Passing a `Buffer` to a [`TypedArray`][] constructor will copy the `Buffer`s
179  contents, interpreted as an array of integers, and not as a byte sequence
180  of the target type.
181
182```js
183const buf = Buffer.from([1, 2, 3, 4]);
184const uint32array = new Uint32Array(buf);
185
186console.log(uint32array);
187
188// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]
189```
190
191* Passing the `Buffer`s underlying [`ArrayBuffer`][] will create a
192  [`TypedArray`][] that shares its memory with the `Buffer`.
193
194```js
195const buf = Buffer.from('hello', 'utf16le');
196const uint16arr = new Uint16Array(
197  buf.buffer,
198  buf.byteOffset,
199  buf.length / Uint16Array.BYTES_PER_ELEMENT);
200
201console.log(uint16array);
202
203// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]
204```
205
206It is possible to create a new `Buffer` that shares the same allocated
207memory as a [`TypedArray`][] instance by using the `TypedArray` object’s
208`.buffer` property in the same way. [`Buffer.from()`][`Buffer.from(arrayBuf)`]
209behaves like `new Uint8Array()` in this context.
210
211```js
212const arr = new Uint16Array(2);
213
214arr[0] = 5000;
215arr[1] = 4000;
216
217// Copies the contents of `arr`.
218const buf1 = Buffer.from(arr);
219
220// Shares memory with `arr`.
221const buf2 = Buffer.from(arr.buffer);
222
223console.log(buf1);
224// Prints: <Buffer 88 a0>
225console.log(buf2);
226// Prints: <Buffer 88 13 a0 0f>
227
228arr[1] = 6000;
229
230console.log(buf1);
231// Prints: <Buffer 88 a0>
232console.log(buf2);
233// Prints: <Buffer 88 13 70 17>
234```
235
236When creating a `Buffer` using a [`TypedArray`][]'s `.buffer`, it is
237possible to use only a portion of the underlying [`ArrayBuffer`][] by passing in
238`byteOffset` and `length` parameters.
239
240```js
241const arr = new Uint16Array(20);
242const buf = Buffer.from(arr.buffer, 0, 16);
243
244console.log(buf.length);
245// Prints: 16
246```
247
248The `Buffer.from()` and [`TypedArray.from()`][] have different signatures and
249implementations. Specifically, the [`TypedArray`][] variants accept a second
250argument that is a mapping function that is invoked on every element of the
251typed array:
252
253* `TypedArray.from(source[, mapFn[, thisArg]])`
254
255The `Buffer.from()` method, however, does not support the use of a mapping
256function:
257
258* [`Buffer.from(array)`][]
259* [`Buffer.from(buffer)`][]
260* [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`]
261* [`Buffer.from(string[, encoding])`][`Buffer.from(string)`]
262
263## Buffers and iteration
264
265`Buffer` instances can be iterated over using `for..of` syntax:
266
267```js
268const buf = Buffer.from([1, 2, 3]);
269
270for (const b of buf) {
271  console.log(b);
272}
273// Prints:
274//   1
275//   2
276//   3
277```
278
279Additionally, the [`buf.values()`][], [`buf.keys()`][], and
280[`buf.entries()`][] methods can be used to create iterators.
281
282## Class: `Buffer`
283
284The `Buffer` class is a global type for dealing with binary data directly.
285It can be constructed in a variety of ways.
286
287### Static method: `Buffer.alloc(size[, fill[, encoding]])`
288<!-- YAML
289added: v5.10.0
290changes:
291  - version: v10.0.0
292    pr-url: https://github.com/nodejs/node/pull/18129
293    description: Attempting to fill a non-zero length buffer with a zero length
294                 buffer triggers a thrown exception.
295  - version: v10.0.0
296    pr-url: https://github.com/nodejs/node/pull/17427
297    description: Specifying an invalid string for `fill` triggers a thrown
298                 exception.
299  - version: v8.9.3
300    pr-url: https://github.com/nodejs/node/pull/17428
301    description: Specifying an invalid string for `fill` now results in a
302                 zero-filled buffer.
303-->
304
305* `size` {integer} The desired length of the new `Buffer`.
306* `fill` {string|Buffer|Uint8Array|integer} A value to pre-fill the new `Buffer`
307  with. **Default:** `0`.
308* `encoding` {string} If `fill` is a string, this is its encoding.
309  **Default:** `'utf8'`.
310
311Allocates a new `Buffer` of `size` bytes. If `fill` is `undefined`, the
312`Buffer` will be zero-filled.
313
314```js
315const buf = Buffer.alloc(5);
316
317console.log(buf);
318// Prints: <Buffer 00 00 00 00 00>
319```
320
321If `size` is larger than
322[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_OPT_VALUE`][]
323is thrown.
324
325If `fill` is specified, the allocated `Buffer` will be initialized by calling
326[`buf.fill(fill)`][`buf.fill()`].
327
328```js
329const buf = Buffer.alloc(5, 'a');
330
331console.log(buf);
332// Prints: <Buffer 61 61 61 61 61>
333```
334
335If both `fill` and `encoding` are specified, the allocated `Buffer` will be
336initialized by calling [`buf.fill(fill, encoding)`][`buf.fill()`].
337
338```js
339const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
340
341console.log(buf);
342// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
343```
344
345Calling [`Buffer.alloc()`][] can be measurably slower than the alternative
346[`Buffer.allocUnsafe()`][] but ensures that the newly created `Buffer` instance
347contents will never contain sensitive data from previous allocations, including
348data that might not have been allocated for `Buffer`s.
349
350A `TypeError` will be thrown if `size` is not a number.
351
352### Static method: `Buffer.allocUnsafe(size)`
353<!-- YAML
354added: v5.10.0
355changes:
356  - version: v7.0.0
357    pr-url: https://github.com/nodejs/node/pull/7079
358    description: Passing a negative `size` will now throw an error.
359-->
360
361* `size` {integer} The desired length of the new `Buffer`.
362
363Allocates a new `Buffer` of `size` bytes. If `size` is larger than
364[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_OPT_VALUE`][]
365is thrown.
366
367The underlying memory for `Buffer` instances created in this way is *not
368initialized*. The contents of the newly created `Buffer` are unknown and
369*may contain sensitive data*. Use [`Buffer.alloc()`][] instead to initialize
370`Buffer` instances with zeroes.
371
372```js
373const buf = Buffer.allocUnsafe(10);
374
375console.log(buf);
376// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>
377
378buf.fill(0);
379
380console.log(buf);
381// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>
382```
383
384A `TypeError` will be thrown if `size` is not a number.
385
386The `Buffer` module pre-allocates an internal `Buffer` instance of
387size [`Buffer.poolSize`][] that is used as a pool for the fast allocation of new
388`Buffer` instances created using [`Buffer.allocUnsafe()`][],
389[`Buffer.from(array)`][], [`Buffer.concat()`][], and the deprecated
390`new Buffer(size)` constructor only when `size` is less than or equal
391to `Buffer.poolSize >> 1` (floor of [`Buffer.poolSize`][] divided by two).
392
393Use of this pre-allocated internal memory pool is a key difference between
394calling `Buffer.alloc(size, fill)` vs. `Buffer.allocUnsafe(size).fill(fill)`.
395Specifically, `Buffer.alloc(size, fill)` will *never* use the internal `Buffer`
396pool, while `Buffer.allocUnsafe(size).fill(fill)` *will* use the internal
397`Buffer` pool if `size` is less than or equal to half [`Buffer.poolSize`][]. The
398difference is subtle but can be important when an application requires the
399additional performance that [`Buffer.allocUnsafe()`][] provides.
400
401### Static method: `Buffer.allocUnsafeSlow(size)`
402<!-- YAML
403added: v5.12.0
404-->
405
406* `size` {integer} The desired length of the new `Buffer`.
407
408Allocates a new `Buffer` of `size` bytes. If `size` is larger than
409[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_OPT_VALUE`][]
410is thrown. A zero-length `Buffer` is created if `size` is 0.
411
412The underlying memory for `Buffer` instances created in this way is *not
413initialized*. The contents of the newly created `Buffer` are unknown and
414*may contain sensitive data*. Use [`buf.fill(0)`][`buf.fill()`] to initialize
415such `Buffer` instances with zeroes.
416
417When using [`Buffer.allocUnsafe()`][] to allocate new `Buffer` instances,
418allocations under 4KB are sliced from a single pre-allocated `Buffer`. This
419allows applications to avoid the garbage collection overhead of creating many
420individually allocated `Buffer` instances. This approach improves both
421performance and memory usage by eliminating the need to track and clean up as
422many individual `ArrayBuffer` objects.
423
424However, in the case where a developer may need to retain a small chunk of
425memory from a pool for an indeterminate amount of time, it may be appropriate
426to create an un-pooled `Buffer` instance using `Buffer.allocUnsafeSlow()` and
427then copying out the relevant bits.
428
429```js
430// Need to keep around a few small chunks of memory.
431const store = [];
432
433socket.on('readable', () => {
434  let data;
435  while (null !== (data = readable.read())) {
436    // Allocate for retained data.
437    const sb = Buffer.allocUnsafeSlow(10);
438
439    // Copy the data into the new allocation.
440    data.copy(sb, 0, 0, 10);
441
442    store.push(sb);
443  }
444});
445```
446
447A `TypeError` will be thrown if `size` is not a number.
448
449### Static method: `Buffer.byteLength(string[, encoding])`
450<!-- YAML
451added: v0.1.90
452changes:
453  - version: v7.0.0
454    pr-url: https://github.com/nodejs/node/pull/8946
455    description: Passing invalid input will now throw an error.
456  - version: v5.10.0
457    pr-url: https://github.com/nodejs/node/pull/5255
458    description: The `string` parameter can now be any `TypedArray`, `DataView`
459                 or `ArrayBuffer`.
460-->
461
462* `string` {string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} A
463  value to calculate the length of.
464* `encoding` {string} If `string` is a string, this is its encoding.
465  **Default:** `'utf8'`.
466* Returns: {integer} The number of bytes contained within `string`.
467
468Returns the byte length of a string when encoded using `encoding`.
469This is not the same as [`String.prototype.length`][], which does not account
470for the encoding that is used to convert the string into bytes.
471
472For `'base64'` and `'hex'`, this function assumes valid input. For strings that
473contain non-base64/hex-encoded data (e.g. whitespace), the return value might be
474greater than the length of a `Buffer` created from the string.
475
476```js
477const str = '\u00bd + \u00bc = \u00be';
478
479console.log(`${str}: ${str.length} characters, ` +
480            `${Buffer.byteLength(str, 'utf8')} bytes`);
481// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes
482```
483
484When `string` is a `Buffer`/[`DataView`][]/[`TypedArray`][]/[`ArrayBuffer`][]/
485[`SharedArrayBuffer`][], the byte length as reported by `.byteLength`
486is returned.
487
488### Static method: `Buffer.compare(buf1, buf2)`
489<!-- YAML
490added: v0.11.13
491changes:
492  - version: v8.0.0
493    pr-url: https://github.com/nodejs/node/pull/10236
494    description: The arguments can now be `Uint8Array`s.
495-->
496
497* `buf1` {Buffer|Uint8Array}
498* `buf2` {Buffer|Uint8Array}
499* Returns: {integer} Either `-1`, `0`, or `1`, depending on the result of the
500  comparison. See [`buf.compare()`][] for details.
501
502Compares `buf1` to `buf2`, typically for the purpose of sorting arrays of
503`Buffer` instances. This is equivalent to calling
504[`buf1.compare(buf2)`][`buf.compare()`].
505
506```js
507const buf1 = Buffer.from('1234');
508const buf2 = Buffer.from('0123');
509const arr = [buf1, buf2];
510
511console.log(arr.sort(Buffer.compare));
512// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]
513// (This result is equal to: [buf2, buf1].)
514```
515
516### Static method: `Buffer.concat(list[, totalLength])`
517<!-- YAML
518added: v0.7.11
519changes:
520  - version: v8.0.0
521    pr-url: https://github.com/nodejs/node/pull/10236
522    description: The elements of `list` can now be `Uint8Array`s.
523-->
524
525* `list` {Buffer[] | Uint8Array[]} List of `Buffer` or [`Uint8Array`][]
526  instances to concatenate.
527* `totalLength` {integer} Total length of the `Buffer` instances in `list`
528  when concatenated.
529* Returns: {Buffer}
530
531Returns a new `Buffer` which is the result of concatenating all the `Buffer`
532instances in the `list` together.
533
534If the list has no items, or if the `totalLength` is 0, then a new zero-length
535`Buffer` is returned.
536
537If `totalLength` is not provided, it is calculated from the `Buffer` instances
538in `list` by adding their lengths.
539
540If `totalLength` is provided, it is coerced to an unsigned integer. If the
541combined length of the `Buffer`s in `list` exceeds `totalLength`, the result is
542truncated to `totalLength`.
543
544```js
545// Create a single `Buffer` from a list of three `Buffer` instances.
546
547const buf1 = Buffer.alloc(10);
548const buf2 = Buffer.alloc(14);
549const buf3 = Buffer.alloc(18);
550const totalLength = buf1.length + buf2.length + buf3.length;
551
552console.log(totalLength);
553// Prints: 42
554
555const bufA = Buffer.concat([buf1, buf2, buf3], totalLength);
556
557console.log(bufA);
558// Prints: <Buffer 00 00 00 00 ...>
559console.log(bufA.length);
560// Prints: 42
561```
562
563`Buffer.concat()` may also use the internal `Buffer` pool like
564[`Buffer.allocUnsafe()`][] does.
565
566### Static method: `Buffer.from(array)`
567<!-- YAML
568added: v5.10.0
569-->
570
571* `array` {integer[]}
572
573Allocates a new `Buffer` using an `array` of bytes in the range `0` – `255`.
574Array entries outside that range will be truncated to fit into it.
575
576```js
577// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.
578const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
579```
580
581A `TypeError` will be thrown if `array` is not an `Array` or another type
582appropriate for `Buffer.from()` variants.
583
584`Buffer.from(array)` and [`Buffer.from(string)`][] may also use the internal
585`Buffer` pool like [`Buffer.allocUnsafe()`][] does.
586
587### Static method: `Buffer.from(arrayBuffer[, byteOffset[, length]])`
588<!-- YAML
589added: v5.10.0
590-->
591
592* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`][],
593  [`SharedArrayBuffer`][], for example the `.buffer` property of a
594  [`TypedArray`][].
595* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`.
596* `length` {integer} Number of bytes to expose.
597  **Default:** `arrayBuffer.byteLength - byteOffset`.
598
599This creates a view of the [`ArrayBuffer`][] without copying the underlying
600memory. For example, when passed a reference to the `.buffer` property of a
601[`TypedArray`][] instance, the newly created `Buffer` will share the same
602allocated memory as the [`TypedArray`][].
603
604```js
605const arr = new Uint16Array(2);
606
607arr[0] = 5000;
608arr[1] = 4000;
609
610// Shares memory with `arr`.
611const buf = Buffer.from(arr.buffer);
612
613console.log(buf);
614// Prints: <Buffer 88 13 a0 0f>
615
616// Changing the original Uint16Array changes the Buffer also.
617arr[1] = 6000;
618
619console.log(buf);
620// Prints: <Buffer 88 13 70 17>
621```
622
623The optional `byteOffset` and `length` arguments specify a memory range within
624the `arrayBuffer` that will be shared by the `Buffer`.
625
626```js
627const ab = new ArrayBuffer(10);
628const buf = Buffer.from(ab, 0, 2);
629
630console.log(buf.length);
631// Prints: 2
632```
633
634A `TypeError` will be thrown if `arrayBuffer` is not an [`ArrayBuffer`][] or a
635[`SharedArrayBuffer`][] or another type appropriate for `Buffer.from()`
636variants.
637
638### Static method: `Buffer.from(buffer)`
639<!-- YAML
640added: v5.10.0
641-->
642
643* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`][] from
644  which to copy data.
645
646Copies the passed `buffer` data onto a new `Buffer` instance.
647
648```js
649const buf1 = Buffer.from('buffer');
650const buf2 = Buffer.from(buf1);
651
652buf1[0] = 0x61;
653
654console.log(buf1.toString());
655// Prints: auffer
656console.log(buf2.toString());
657// Prints: buffer
658```
659
660A `TypeError` will be thrown if `buffer` is not a `Buffer` or another type
661appropriate for `Buffer.from()` variants.
662
663### Static method: `Buffer.from(object[, offsetOrEncoding[, length]])`
664<!-- YAML
665added: v8.2.0
666-->
667
668* `object` {Object} An object supporting `Symbol.toPrimitive` or `valueOf()`.
669* `offsetOrEncoding` {integer|string} A byte-offset or encoding.
670* `length` {integer} A length.
671
672For objects whose `valueOf()` function returns a value not strictly equal to
673`object`, returns `Buffer.from(object.valueOf(), offsetOrEncoding, length)`.
674
675```js
676const buf = Buffer.from(new String('this is a test'));
677// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
678```
679
680For objects that support `Symbol.toPrimitive`, returns
681`Buffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding)`.
682
683```js
684class Foo {
685  [Symbol.toPrimitive]() {
686    return 'this is a test';
687  }
688}
689
690const buf = Buffer.from(new Foo(), 'utf8');
691// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
692```
693
694A `TypeError` will be thrown if `object` does not have the mentioned methods or
695is not of another type appropriate for `Buffer.from()` variants.
696
697### Static method: `Buffer.from(string[, encoding])`
698<!-- YAML
699added: v5.10.0
700-->
701
702* `string` {string} A string to encode.
703* `encoding` {string} The encoding of `string`. **Default:** `'utf8'`.
704
705Creates a new `Buffer` containing `string`. The `encoding` parameter identifies
706the character encoding to be used when converting `string` into bytes.
707
708```js
709const buf1 = Buffer.from('this is a tést');
710const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
711
712console.log(buf1.toString());
713// Prints: this is a tést
714console.log(buf2.toString());
715// Prints: this is a tést
716console.log(buf1.toString('latin1'));
717// Prints: this is a tést
718```
719
720A `TypeError` will be thrown if `string` is not a string or another type
721appropriate for `Buffer.from()` variants.
722
723### Static method: `Buffer.isBuffer(obj)`
724<!-- YAML
725added: v0.1.101
726-->
727
728* `obj` {Object}
729* Returns: {boolean}
730
731Returns `true` if `obj` is a `Buffer`, `false` otherwise.
732
733### Static method: `Buffer.isEncoding(encoding)`
734<!-- YAML
735added: v0.9.1
736-->
737
738* `encoding` {string} A character encoding name to check.
739* Returns: {boolean}
740
741Returns `true` if `encoding` is the name of a supported character encoding,
742or `false` otherwise.
743
744```js
745console.log(Buffer.isEncoding('utf-8'));
746// Prints: true
747
748console.log(Buffer.isEncoding('hex'));
749// Prints: true
750
751console.log(Buffer.isEncoding('utf/8'));
752// Prints: false
753
754console.log(Buffer.isEncoding(''));
755// Prints: false
756```
757
758### Class property: `Buffer.poolSize`
759<!-- YAML
760added: v0.11.3
761-->
762
763* {integer} **Default:** `8192`
764
765This is the size (in bytes) of pre-allocated internal `Buffer` instances used
766for pooling. This value may be modified.
767
768### `buf[index]`
769<!-- YAML
770type: property
771name: [index]
772-->
773
774* `index` {integer}
775
776The index operator `[index]` can be used to get and set the octet at position
777`index` in `buf`. The values refer to individual bytes, so the legal value
778range is between `0x00` and `0xFF` (hex) or `0` and `255` (decimal).
779
780This operator is inherited from `Uint8Array`, so its behavior on out-of-bounds
781access is the same as `Uint8Array`. In other words, `buf[index]` returns
782`undefined` when `index` is negative or greater or equal to `buf.length`, and
783`buf[index] = value` does not modify the buffer if `index` is negative or
784`>= buf.length`.
785
786```js
787// Copy an ASCII string into a `Buffer` one byte at a time.
788// (This only works for ASCII-only strings. In general, one should use
789// `Buffer.from()` to perform this conversion.)
790
791const str = 'Node.js';
792const buf = Buffer.allocUnsafe(str.length);
793
794for (let i = 0; i < str.length; i++) {
795  buf[i] = str.charCodeAt(i);
796}
797
798console.log(buf.toString('utf8'));
799// Prints: Node.js
800```
801
802### `buf.buffer`
803
804* {ArrayBuffer} The underlying `ArrayBuffer` object based on which this `Buffer`
805  object is created.
806
807This `ArrayBuffer` is not guaranteed to correspond exactly to the original
808`Buffer`. See the notes on `buf.byteOffset` for details.
809
810```js
811const arrayBuffer = new ArrayBuffer(16);
812const buffer = Buffer.from(arrayBuffer);
813
814console.log(buffer.buffer === arrayBuffer);
815// Prints: true
816```
817
818### `buf.byteOffset`
819
820* {integer} The `byteOffset` of the `Buffer`s underlying `ArrayBuffer` object.
821
822When setting `byteOffset` in `Buffer.from(ArrayBuffer, byteOffset, length)`,
823or sometimes when allocating a `Buffer` smaller than `Buffer.poolSize`, the
824buffer does not start from a zero offset on the underlying `ArrayBuffer`.
825
826This can cause problems when accessing the underlying `ArrayBuffer` directly
827using `buf.buffer`, as other parts of the `ArrayBuffer` may be unrelated
828to the `Buffer` object itself.
829
830A common issue when creating a `TypedArray` object that shares its memory with
831a `Buffer` is that in this case one needs to specify the `byteOffset` correctly:
832
833```js
834// Create a buffer smaller than `Buffer.poolSize`.
835const nodeBuffer = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
836
837// When casting the Node.js Buffer to an Int8Array, use the byteOffset
838// to refer only to the part of `nodeBuffer.buffer` that contains the memory
839// for `nodeBuffer`.
840new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);
841```
842
843### `buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])`
844<!-- YAML
845added: v0.11.13
846changes:
847  - version: v8.0.0
848    pr-url: https://github.com/nodejs/node/pull/10236
849    description: The `target` parameter can now be a `Uint8Array`.
850  - version: v5.11.0
851    pr-url: https://github.com/nodejs/node/pull/5880
852    description: Additional parameters for specifying offsets are supported now.
853-->
854
855* `target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] with which to
856  compare `buf`.
857* `targetStart` {integer} The offset within `target` at which to begin
858  comparison. **Default:** `0`.
859* `targetEnd` {integer} The offset within `target` at which to end comparison
860  (not inclusive). **Default:** `target.length`.
861* `sourceStart` {integer} The offset within `buf` at which to begin comparison.
862  **Default:** `0`.
863* `sourceEnd` {integer} The offset within `buf` at which to end comparison
864  (not inclusive). **Default:** [`buf.length`][].
865* Returns: {integer}
866
867Compares `buf` with `target` and returns a number indicating whether `buf`
868comes before, after, or is the same as `target` in sort order.
869Comparison is based on the actual sequence of bytes in each `Buffer`.
870
871* `0` is returned if `target` is the same as `buf`
872* `1` is returned if `target` should come *before* `buf` when sorted.
873* `-1` is returned if `target` should come *after* `buf` when sorted.
874
875```js
876const buf1 = Buffer.from('ABC');
877const buf2 = Buffer.from('BCD');
878const buf3 = Buffer.from('ABCD');
879
880console.log(buf1.compare(buf1));
881// Prints: 0
882console.log(buf1.compare(buf2));
883// Prints: -1
884console.log(buf1.compare(buf3));
885// Prints: -1
886console.log(buf2.compare(buf1));
887// Prints: 1
888console.log(buf2.compare(buf3));
889// Prints: 1
890console.log([buf1, buf2, buf3].sort(Buffer.compare));
891// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]
892// (This result is equal to: [buf1, buf3, buf2].)
893```
894
895The optional `targetStart`, `targetEnd`, `sourceStart`, and `sourceEnd`
896arguments can be used to limit the comparison to specific ranges within `target`
897and `buf` respectively.
898
899```js
900const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
901const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);
902
903console.log(buf1.compare(buf2, 5, 9, 0, 4));
904// Prints: 0
905console.log(buf1.compare(buf2, 0, 6, 4));
906// Prints: -1
907console.log(buf1.compare(buf2, 5, 6, 5));
908// Prints: 1
909```
910
911[`ERR_OUT_OF_RANGE`][] is thrown if `targetStart < 0`, `sourceStart < 0`,
912`targetEnd > target.byteLength`, or `sourceEnd > source.byteLength`.
913
914### `buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])`
915<!-- YAML
916added: v0.1.90
917-->
918
919* `target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] to copy into.
920* `targetStart` {integer} The offset within `target` at which to begin
921  writing. **Default:** `0`.
922* `sourceStart` {integer} The offset within `buf` from which to begin copying.
923  **Default:** `0`.
924* `sourceEnd` {integer} The offset within `buf` at which to stop copying (not
925  inclusive). **Default:** [`buf.length`][].
926* Returns: {integer} The number of bytes copied.
927
928Copies data from a region of `buf` to a region in `target`, even if the `target`
929memory region overlaps with `buf`.
930
931[`TypedArray#set()`][] performs the same operation, and is available for all
932TypedArrays, including Node.js `Buffer`s, although it takes different
933function arguments.
934
935```js
936// Create two `Buffer` instances.
937const buf1 = Buffer.allocUnsafe(26);
938const buf2 = Buffer.allocUnsafe(26).fill('!');
939
940for (let i = 0; i < 26; i++) {
941  // 97 is the decimal ASCII value for 'a'.
942  buf1[i] = i + 97;
943}
944
945// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.
946buf1.copy(buf2, 8, 16, 20);
947// This is equivalent to:
948// buf2.set(buf1.subarray(16, 20), 8);
949
950console.log(buf2.toString('ascii', 0, 25));
951// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
952```
953
954```js
955// Create a `Buffer` and copy data from one region to an overlapping region
956// within the same `Buffer`.
957
958const buf = Buffer.allocUnsafe(26);
959
960for (let i = 0; i < 26; i++) {
961  // 97 is the decimal ASCII value for 'a'.
962  buf[i] = i + 97;
963}
964
965buf.copy(buf, 0, 4, 10);
966
967console.log(buf.toString());
968// Prints: efghijghijklmnopqrstuvwxyz
969```
970
971### `buf.entries()`
972<!-- YAML
973added: v1.1.0
974-->
975
976* Returns: {Iterator}
977
978Creates and returns an [iterator][] of `[index, byte]` pairs from the contents
979of `buf`.
980
981```js
982// Log the entire contents of a `Buffer`.
983
984const buf = Buffer.from('buffer');
985
986for (const pair of buf.entries()) {
987  console.log(pair);
988}
989// Prints:
990//   [0, 98]
991//   [1, 117]
992//   [2, 102]
993//   [3, 102]
994//   [4, 101]
995//   [5, 114]
996```
997
998### `buf.equals(otherBuffer)`
999<!-- YAML
1000added: v0.11.13
1001changes:
1002  - version: v8.0.0
1003    pr-url: https://github.com/nodejs/node/pull/10236
1004    description: The arguments can now be `Uint8Array`s.
1005-->
1006
1007* `otherBuffer` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] with which to
1008  compare `buf`.
1009* Returns: {boolean}
1010
1011Returns `true` if both `buf` and `otherBuffer` have exactly the same bytes,
1012`false` otherwise. Equivalent to
1013[`buf.compare(otherBuffer) === 0`][`buf.compare()`].
1014
1015```js
1016const buf1 = Buffer.from('ABC');
1017const buf2 = Buffer.from('414243', 'hex');
1018const buf3 = Buffer.from('ABCD');
1019
1020console.log(buf1.equals(buf2));
1021// Prints: true
1022console.log(buf1.equals(buf3));
1023// Prints: false
1024```
1025
1026### `buf.fill(value[, offset[, end]][, encoding])`
1027<!-- YAML
1028added: v0.5.0
1029changes:
1030  - version: v11.0.0
1031    pr-url: https://github.com/nodejs/node/pull/22969
1032    description: Throws `ERR_OUT_OF_RANGE` instead of `ERR_INDEX_OUT_OF_RANGE`.
1033  - version: v10.0.0
1034    pr-url: https://github.com/nodejs/node/pull/18790
1035    description: Negative `end` values throw an `ERR_INDEX_OUT_OF_RANGE` error.
1036  - version: v10.0.0
1037    pr-url: https://github.com/nodejs/node/pull/18129
1038    description: Attempting to fill a non-zero length buffer with a zero length
1039                 buffer triggers a thrown exception.
1040  - version: v10.0.0
1041    pr-url: https://github.com/nodejs/node/pull/17427
1042    description: Specifying an invalid string for `value` triggers a thrown
1043                 exception.
1044  - version: v5.7.0
1045    pr-url: https://github.com/nodejs/node/pull/4935
1046    description: The `encoding` parameter is supported now.
1047-->
1048
1049* `value` {string|Buffer|Uint8Array|integer} The value with which to fill `buf`.
1050* `offset` {integer} Number of bytes to skip before starting to fill `buf`.
1051  **Default:** `0`.
1052* `end` {integer} Where to stop filling `buf` (not inclusive). **Default:**
1053  [`buf.length`][].
1054* `encoding` {string} The encoding for `value` if `value` is a string.
1055  **Default:** `'utf8'`.
1056* Returns: {Buffer} A reference to `buf`.
1057
1058Fills `buf` with the specified `value`. If the `offset` and `end` are not given,
1059the entire `buf` will be filled:
1060
1061```js
1062// Fill a `Buffer` with the ASCII character 'h'.
1063
1064const b = Buffer.allocUnsafe(50).fill('h');
1065
1066console.log(b.toString());
1067// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
1068```
1069
1070`value` is coerced to a `uint32` value if it is not a string, `Buffer`, or
1071integer. If the resulting integer is greater than `255` (decimal), `buf` will be
1072filled with `value & 255`.
1073
1074If the final write of a `fill()` operation falls on a multi-byte character,
1075then only the bytes of that character that fit into `buf` are written:
1076
1077```js
1078// Fill a `Buffer` with character that takes up two bytes in UTF-8.
1079
1080console.log(Buffer.allocUnsafe(5).fill('\u0222'));
1081// Prints: <Buffer c8 a2 c8 a2 c8>
1082```
1083
1084If `value` contains invalid characters, it is truncated; if no valid
1085fill data remains, an exception is thrown:
1086
1087```js
1088const buf = Buffer.allocUnsafe(5);
1089
1090console.log(buf.fill('a'));
1091// Prints: <Buffer 61 61 61 61 61>
1092console.log(buf.fill('aazz', 'hex'));
1093// Prints: <Buffer aa aa aa aa aa>
1094console.log(buf.fill('zz', 'hex'));
1095// Throws an exception.
1096```
1097
1098### `buf.includes(value[, byteOffset][, encoding])`
1099<!-- YAML
1100added: v5.3.0
1101-->
1102
1103* `value` {string|Buffer|Uint8Array|integer} What to search for.
1104* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then
1105  offset is calculated from the end of `buf`. **Default:** `0`.
1106* `encoding` {string} If `value` is a string, this is its encoding.
1107  **Default:** `'utf8'`.
1108* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
1109
1110Equivalent to [`buf.indexOf() !== -1`][`buf.indexOf()`].
1111
1112```js
1113const buf = Buffer.from('this is a buffer');
1114
1115console.log(buf.includes('this'));
1116// Prints: true
1117console.log(buf.includes('is'));
1118// Prints: true
1119console.log(buf.includes(Buffer.from('a buffer')));
1120// Prints: true
1121console.log(buf.includes(97));
1122// Prints: true (97 is the decimal ASCII value for 'a')
1123console.log(buf.includes(Buffer.from('a buffer example')));
1124// Prints: false
1125console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));
1126// Prints: true
1127console.log(buf.includes('this', 4));
1128// Prints: false
1129```
1130
1131### `buf.indexOf(value[, byteOffset][, encoding])`
1132<!-- YAML
1133added: v1.5.0
1134changes:
1135  - version: v8.0.0
1136    pr-url: https://github.com/nodejs/node/pull/10236
1137    description: The `value` can now be a `Uint8Array`.
1138  - version: v5.7.0, v4.4.0
1139    pr-url: https://github.com/nodejs/node/pull/4803
1140    description: When `encoding` is being passed, the `byteOffset` parameter
1141                 is no longer required.
1142-->
1143
1144* `value` {string|Buffer|Uint8Array|integer} What to search for.
1145* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then
1146  offset is calculated from the end of `buf`. **Default:** `0`.
1147* `encoding` {string} If `value` is a string, this is the encoding used to
1148  determine the binary representation of the string that will be searched for in
1149  `buf`. **Default:** `'utf8'`.
1150* Returns: {integer} The index of the first occurrence of `value` in `buf`, or
1151  `-1` if `buf` does not contain `value`.
1152
1153If `value` is:
1154
1155* a string, `value` is interpreted according to the character encoding in
1156  `encoding`.
1157* a `Buffer` or [`Uint8Array`][], `value` will be used in its entirety.
1158  To compare a partial `Buffer`, use [`buf.slice()`][].
1159* a number, `value` will be interpreted as an unsigned 8-bit integer
1160  value between `0` and `255`.
1161
1162```js
1163const buf = Buffer.from('this is a buffer');
1164
1165console.log(buf.indexOf('this'));
1166// Prints: 0
1167console.log(buf.indexOf('is'));
1168// Prints: 2
1169console.log(buf.indexOf(Buffer.from('a buffer')));
1170// Prints: 8
1171console.log(buf.indexOf(97));
1172// Prints: 8 (97 is the decimal ASCII value for 'a')
1173console.log(buf.indexOf(Buffer.from('a buffer example')));
1174// Prints: -1
1175console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));
1176// Prints: 8
1177
1178const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
1179
1180console.log(utf16Buffer.indexOf('\u03a3', 0, 'utf16le'));
1181// Prints: 4
1182console.log(utf16Buffer.indexOf('\u03a3', -4, 'utf16le'));
1183// Prints: 6
1184```
1185
1186If `value` is not a string, number, or `Buffer`, this method will throw a
1187`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
1188an integer between 0 and 255.
1189
1190If `byteOffset` is not a number, it will be coerced to a number. If the result
1191of coercion is `NaN` or `0`, then the entire buffer will be searched. This
1192behavior matches [`String#indexOf()`][].
1193
1194```js
1195const b = Buffer.from('abcdef');
1196
1197// Passing a value that's a number, but not a valid byte.
1198// Prints: 2, equivalent to searching for 99 or 'c'.
1199console.log(b.indexOf(99.9));
1200console.log(b.indexOf(256 + 99));
1201
1202// Passing a byteOffset that coerces to NaN or 0.
1203// Prints: 1, searching the whole buffer.
1204console.log(b.indexOf('b', undefined));
1205console.log(b.indexOf('b', {}));
1206console.log(b.indexOf('b', null));
1207console.log(b.indexOf('b', []));
1208```
1209
1210If `value` is an empty string or empty `Buffer` and `byteOffset` is less
1211than `buf.length`, `byteOffset` will be returned. If `value` is empty and
1212`byteOffset` is at least `buf.length`, `buf.length` will be returned.
1213
1214### `buf.keys()`
1215<!-- YAML
1216added: v1.1.0
1217-->
1218
1219* Returns: {Iterator}
1220
1221Creates and returns an [iterator][] of `buf` keys (indices).
1222
1223```js
1224const buf = Buffer.from('buffer');
1225
1226for (const key of buf.keys()) {
1227  console.log(key);
1228}
1229// Prints:
1230//   0
1231//   1
1232//   2
1233//   3
1234//   4
1235//   5
1236```
1237
1238### `buf.lastIndexOf(value[, byteOffset][, encoding])`
1239<!-- YAML
1240added: v6.0.0
1241changes:
1242  - version: v8.0.0
1243    pr-url: https://github.com/nodejs/node/pull/10236
1244    description: The `value` can now be a `Uint8Array`.
1245-->
1246
1247* `value` {string|Buffer|Uint8Array|integer} What to search for.
1248* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then
1249  offset is calculated from the end of `buf`. **Default:**
1250  `buf.length - 1`.
1251* `encoding` {string} If `value` is a string, this is the encoding used to
1252  determine the binary representation of the string that will be searched for in
1253  `buf`. **Default:** `'utf8'`.
1254* Returns: {integer} The index of the last occurrence of `value` in `buf`, or
1255  `-1` if `buf` does not contain `value`.
1256
1257Identical to [`buf.indexOf()`][], except the last occurrence of `value` is found
1258rather than the first occurrence.
1259
1260```js
1261const buf = Buffer.from('this buffer is a buffer');
1262
1263console.log(buf.lastIndexOf('this'));
1264// Prints: 0
1265console.log(buf.lastIndexOf('buffer'));
1266// Prints: 17
1267console.log(buf.lastIndexOf(Buffer.from('buffer')));
1268// Prints: 17
1269console.log(buf.lastIndexOf(97));
1270// Prints: 15 (97 is the decimal ASCII value for 'a')
1271console.log(buf.lastIndexOf(Buffer.from('yolo')));
1272// Prints: -1
1273console.log(buf.lastIndexOf('buffer', 5));
1274// Prints: 5
1275console.log(buf.lastIndexOf('buffer', 4));
1276// Prints: -1
1277
1278const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
1279
1280console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le'));
1281// Prints: 6
1282console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le'));
1283// Prints: 4
1284```
1285
1286If `value` is not a string, number, or `Buffer`, this method will throw a
1287`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
1288an integer between 0 and 255.
1289
1290If `byteOffset` is not a number, it will be coerced to a number. Any arguments
1291that coerce to `NaN`, like `{}` or `undefined`, will search the whole buffer.
1292This behavior matches [`String#lastIndexOf()`][].
1293
1294```js
1295const b = Buffer.from('abcdef');
1296
1297// Passing a value that's a number, but not a valid byte.
1298// Prints: 2, equivalent to searching for 99 or 'c'.
1299console.log(b.lastIndexOf(99.9));
1300console.log(b.lastIndexOf(256 + 99));
1301
1302// Passing a byteOffset that coerces to NaN.
1303// Prints: 1, searching the whole buffer.
1304console.log(b.lastIndexOf('b', undefined));
1305console.log(b.lastIndexOf('b', {}));
1306
1307// Passing a byteOffset that coerces to 0.
1308// Prints: -1, equivalent to passing 0.
1309console.log(b.lastIndexOf('b', null));
1310console.log(b.lastIndexOf('b', []));
1311```
1312
1313If `value` is an empty string or empty `Buffer`, `byteOffset` will be returned.
1314
1315### `buf.length`
1316<!-- YAML
1317added: v0.1.90
1318-->
1319
1320* {integer}
1321
1322Returns the number of bytes in `buf`.
1323
1324```js
1325// Create a `Buffer` and write a shorter string to it using UTF-8.
1326
1327const buf = Buffer.alloc(1234);
1328
1329console.log(buf.length);
1330// Prints: 1234
1331
1332buf.write('some string', 0, 'utf8');
1333
1334console.log(buf.length);
1335// Prints: 1234
1336```
1337
1338### `buf.parent`
1339<!-- YAML
1340deprecated: v8.0.0
1341-->
1342
1343> Stability: 0 - Deprecated: Use [`buf.buffer`][] instead.
1344
1345The `buf.parent` property is a deprecated alias for `buf.buffer`.
1346
1347### `buf.readBigInt64BE([offset])`
1348<!-- YAML
1349added:
1350 - v12.0.0
1351 - v10.20.0
1352-->
1353
1354* `offset` {integer} Number of bytes to skip before starting to read. Must
1355  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
1356* Returns: {bigint}
1357
1358Reads a signed, big-endian 64-bit integer from `buf` at the specified `offset`.
1359
1360Integers read from a `Buffer` are interpreted as two's complement signed
1361values.
1362
1363### `buf.readBigInt64LE([offset])`
1364<!-- YAML
1365added: v12.0.0
1366-->
1367
1368* `offset` {integer} Number of bytes to skip before starting to read. Must
1369  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
1370* Returns: {bigint}
1371
1372Reads a signed, little-endian 64-bit integer from `buf` at the specified
1373`offset`.
1374
1375Integers read from a `Buffer` are interpreted as two's complement signed
1376values.
1377
1378### `buf.readBigUInt64BE([offset])`
1379<!-- YAML
1380added: v12.0.0
1381changes:
1382  - version: v12.19.0
1383    pr-url: https://github.com/nodejs/node/pull/34960
1384    description: This function is also available as `buf.readBigUint64BE()`.
1385-->
1386
1387* `offset` {integer} Number of bytes to skip before starting to read. Must
1388  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
1389* Returns: {bigint}
1390
1391Reads an unsigned, big-endian 64-bit integer from `buf` at the specified
1392`offset`.
1393
1394```js
1395const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
1396
1397console.log(buf.readBigUInt64BE(0));
1398// Prints: 4294967295n
1399```
1400
1401### `buf.readBigUInt64LE([offset])`
1402<!-- YAML
1403added:
1404 - v12.0.0
1405 - v10.20.0
1406changes:
1407  - version: v12.19.0
1408    pr-url: https://github.com/nodejs/node/pull/34960
1409    description: This function is also available as `buf.readBigUint64LE()`.
1410-->
1411
1412* `offset` {integer} Number of bytes to skip before starting to read. Must
1413  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
1414* Returns: {bigint}
1415
1416Reads an unsigned, little-endian 64-bit integer from `buf` at the specified
1417`offset`.
1418
1419```js
1420const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
1421
1422console.log(buf.readBigUInt64LE(0));
1423// Prints: 18446744069414584320n
1424```
1425
1426### `buf.readDoubleBE([offset])`
1427<!-- YAML
1428added: v0.11.15
1429changes:
1430  - version: v10.0.0
1431    pr-url: https://github.com/nodejs/node/pull/18395
1432    description: Removed `noAssert` and no implicit coercion of the offset
1433                 to `uint32` anymore.
1434-->
1435
1436* `offset` {integer} Number of bytes to skip before starting to read. Must
1437  satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.
1438* Returns: {number}
1439
1440Reads a 64-bit, big-endian double from `buf` at the specified `offset`.
1441
1442```js
1443const buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
1444
1445console.log(buf.readDoubleBE(0));
1446// Prints: 8.20788039913184e-304
1447```
1448
1449### `buf.readDoubleLE([offset])`
1450<!-- YAML
1451added: v0.11.15
1452changes:
1453  - version: v10.0.0
1454    pr-url: https://github.com/nodejs/node/pull/18395
1455    description: Removed `noAssert` and no implicit coercion of the offset
1456                 to `uint32` anymore.
1457-->
1458
1459* `offset` {integer} Number of bytes to skip before starting to read. Must
1460  satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.
1461* Returns: {number}
1462
1463Reads a 64-bit, little-endian double from `buf` at the specified `offset`.
1464
1465```js
1466const buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
1467
1468console.log(buf.readDoubleLE(0));
1469// Prints: 5.447603722011605e-270
1470console.log(buf.readDoubleLE(1));
1471// Throws ERR_OUT_OF_RANGE.
1472```
1473
1474### `buf.readFloatBE([offset])`
1475<!-- YAML
1476added: v0.11.15
1477changes:
1478  - version: v10.0.0
1479    pr-url: https://github.com/nodejs/node/pull/18395
1480    description: Removed `noAssert` and no implicit coercion of the offset
1481                 to `uint32` anymore.
1482-->
1483
1484* `offset` {integer} Number of bytes to skip before starting to read. Must
1485  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
1486* Returns: {number}
1487
1488Reads a 32-bit, big-endian float from `buf` at the specified `offset`.
1489
1490```js
1491const buf = Buffer.from([1, 2, 3, 4]);
1492
1493console.log(buf.readFloatBE(0));
1494// Prints: 2.387939260590663e-38
1495```
1496
1497### `buf.readFloatLE([offset])`
1498<!-- YAML
1499added: v0.11.15
1500changes:
1501  - version: v10.0.0
1502    pr-url: https://github.com/nodejs/node/pull/18395
1503    description: Removed `noAssert` and no implicit coercion of the offset
1504                 to `uint32` anymore.
1505-->
1506
1507* `offset` {integer} Number of bytes to skip before starting to read. Must
1508  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
1509* Returns: {number}
1510
1511Reads a 32-bit, little-endian float from `buf` at the specified `offset`.
1512
1513```js
1514const buf = Buffer.from([1, 2, 3, 4]);
1515
1516console.log(buf.readFloatLE(0));
1517// Prints: 1.539989614439558e-36
1518console.log(buf.readFloatLE(1));
1519// Throws ERR_OUT_OF_RANGE.
1520```
1521
1522### `buf.readInt8([offset])`
1523<!-- YAML
1524added: v0.5.0
1525changes:
1526  - version: v10.0.0
1527    pr-url: https://github.com/nodejs/node/pull/18395
1528    description: Removed `noAssert` and no implicit coercion of the offset
1529                 to `uint32` anymore.
1530-->
1531
1532* `offset` {integer} Number of bytes to skip before starting to read. Must
1533  satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.
1534* Returns: {integer}
1535
1536Reads a signed 8-bit integer from `buf` at the specified `offset`.
1537
1538Integers read from a `Buffer` are interpreted as two's complement signed values.
1539
1540```js
1541const buf = Buffer.from([-1, 5]);
1542
1543console.log(buf.readInt8(0));
1544// Prints: -1
1545console.log(buf.readInt8(1));
1546// Prints: 5
1547console.log(buf.readInt8(2));
1548// Throws ERR_OUT_OF_RANGE.
1549```
1550
1551### `buf.readInt16BE([offset])`
1552<!-- YAML
1553added: v0.5.5
1554changes:
1555  - version: v10.0.0
1556    pr-url: https://github.com/nodejs/node/pull/18395
1557    description: Removed `noAssert` and no implicit coercion of the offset
1558                 to `uint32` anymore.
1559-->
1560
1561* `offset` {integer} Number of bytes to skip before starting to read. Must
1562  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
1563* Returns: {integer}
1564
1565Reads a signed, big-endian 16-bit integer from `buf` at the specified `offset`.
1566
1567Integers read from a `Buffer` are interpreted as two's complement signed values.
1568
1569```js
1570const buf = Buffer.from([0, 5]);
1571
1572console.log(buf.readInt16BE(0));
1573// Prints: 5
1574```
1575
1576### `buf.readInt16LE([offset])`
1577<!-- YAML
1578added: v0.5.5
1579changes:
1580  - version: v10.0.0
1581    pr-url: https://github.com/nodejs/node/pull/18395
1582    description: Removed `noAssert` and no implicit coercion of the offset
1583                 to `uint32` anymore.
1584-->
1585
1586* `offset` {integer} Number of bytes to skip before starting to read. Must
1587  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
1588* Returns: {integer}
1589
1590Reads a signed, little-endian 16-bit integer from `buf` at the specified
1591`offset`.
1592
1593Integers read from a `Buffer` are interpreted as two's complement signed values.
1594
1595```js
1596const buf = Buffer.from([0, 5]);
1597
1598console.log(buf.readInt16LE(0));
1599// Prints: 1280
1600console.log(buf.readInt16LE(1));
1601// Throws ERR_OUT_OF_RANGE.
1602```
1603
1604### `buf.readInt32BE([offset])`
1605<!-- YAML
1606added: v0.5.5
1607changes:
1608  - version: v10.0.0
1609    pr-url: https://github.com/nodejs/node/pull/18395
1610    description: Removed `noAssert` and no implicit coercion of the offset
1611                 to `uint32` anymore.
1612-->
1613
1614* `offset` {integer} Number of bytes to skip before starting to read. Must
1615  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
1616* Returns: {integer}
1617
1618Reads a signed, big-endian 32-bit integer from `buf` at the specified `offset`.
1619
1620Integers read from a `Buffer` are interpreted as two's complement signed values.
1621
1622```js
1623const buf = Buffer.from([0, 0, 0, 5]);
1624
1625console.log(buf.readInt32BE(0));
1626// Prints: 5
1627```
1628
1629### `buf.readInt32LE([offset])`
1630<!-- YAML
1631added: v0.5.5
1632changes:
1633  - version: v10.0.0
1634    pr-url: https://github.com/nodejs/node/pull/18395
1635    description: Removed `noAssert` and no implicit coercion of the offset
1636                 to `uint32` anymore.
1637-->
1638
1639* `offset` {integer} Number of bytes to skip before starting to read. Must
1640  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
1641* Returns: {integer}
1642
1643Reads a signed, little-endian 32-bit integer from `buf` at the specified
1644`offset`.
1645
1646Integers read from a `Buffer` are interpreted as two's complement signed values.
1647
1648```js
1649const buf = Buffer.from([0, 0, 0, 5]);
1650
1651console.log(buf.readInt32LE(0));
1652// Prints: 83886080
1653console.log(buf.readInt32LE(1));
1654// Throws ERR_OUT_OF_RANGE.
1655```
1656
1657### `buf.readIntBE(offset, byteLength)`
1658<!-- YAML
1659added: v0.11.15
1660changes:
1661  - version: v10.0.0
1662    pr-url: https://github.com/nodejs/node/pull/18395
1663    description: Removed `noAssert` and no implicit coercion of the offset
1664                 and `byteLength` to `uint32` anymore.
1665-->
1666
1667* `offset` {integer} Number of bytes to skip before starting to read. Must
1668  satisfy `0 <= offset <= buf.length - byteLength`.
1669* `byteLength` {integer} Number of bytes to read. Must satisfy
1670  `0 < byteLength <= 6`.
1671* Returns: {integer}
1672
1673Reads `byteLength` number of bytes from `buf` at the specified `offset`
1674and interprets the result as a big-endian, two's complement signed value
1675supporting up to 48 bits of accuracy.
1676
1677```js
1678const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
1679
1680console.log(buf.readIntBE(0, 6).toString(16));
1681// Prints: 1234567890ab
1682console.log(buf.readIntBE(1, 6).toString(16));
1683// Throws ERR_OUT_OF_RANGE.
1684console.log(buf.readIntBE(1, 0).toString(16));
1685// Throws ERR_OUT_OF_RANGE.
1686```
1687
1688### `buf.readIntLE(offset, byteLength)`
1689<!-- YAML
1690added: v0.11.15
1691changes:
1692  - version: v10.0.0
1693    pr-url: https://github.com/nodejs/node/pull/18395
1694    description: Removed `noAssert` and no implicit coercion of the offset
1695                 and `byteLength` to `uint32` anymore.
1696-->
1697
1698* `offset` {integer} Number of bytes to skip before starting to read. Must
1699  satisfy `0 <= offset <= buf.length - byteLength`.
1700* `byteLength` {integer} Number of bytes to read. Must satisfy
1701  `0 < byteLength <= 6`.
1702* Returns: {integer}
1703
1704Reads `byteLength` number of bytes from `buf` at the specified `offset`
1705and interprets the result as a little-endian, two's complement signed value
1706supporting up to 48 bits of accuracy.
1707
1708```js
1709const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
1710
1711console.log(buf.readIntLE(0, 6).toString(16));
1712// Prints: -546f87a9cbee
1713```
1714
1715### `buf.readUInt8([offset])`
1716<!-- YAML
1717added: v0.5.0
1718changes:
1719  - version: v12.19.0
1720    pr-url: https://github.com/nodejs/node/pull/34729
1721    description: This function is also available as `buf.readUint8()`.
1722  - version: v10.0.0
1723    pr-url: https://github.com/nodejs/node/pull/18395
1724    description: Removed `noAssert` and no implicit coercion of the offset
1725                 to `uint32` anymore.
1726-->
1727
1728* `offset` {integer} Number of bytes to skip before starting to read. Must
1729  satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.
1730* Returns: {integer}
1731
1732Reads an unsigned 8-bit integer from `buf` at the specified `offset`.
1733
1734```js
1735const buf = Buffer.from([1, -2]);
1736
1737console.log(buf.readUInt8(0));
1738// Prints: 1
1739console.log(buf.readUInt8(1));
1740// Prints: 254
1741console.log(buf.readUInt8(2));
1742// Throws ERR_OUT_OF_RANGE.
1743```
1744
1745### `buf.readUInt16BE([offset])`
1746<!-- YAML
1747added: v0.5.5
1748changes:
1749  - version: v12.19.0
1750    pr-url: https://github.com/nodejs/node/pull/34729
1751    description: This function is also available as `buf.readUint16BE()`.
1752  - version: v10.0.0
1753    pr-url: https://github.com/nodejs/node/pull/18395
1754    description: Removed `noAssert` and no implicit coercion of the offset
1755                 to `uint32` anymore.
1756-->
1757
1758* `offset` {integer} Number of bytes to skip before starting to read. Must
1759  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
1760* Returns: {integer}
1761
1762Reads an unsigned, big-endian 16-bit integer from `buf` at the specified
1763`offset`.
1764
1765```js
1766const buf = Buffer.from([0x12, 0x34, 0x56]);
1767
1768console.log(buf.readUInt16BE(0).toString(16));
1769// Prints: 1234
1770console.log(buf.readUInt16BE(1).toString(16));
1771// Prints: 3456
1772```
1773
1774### `buf.readUInt16LE([offset])`
1775<!-- YAML
1776added: v0.5.5
1777changes:
1778  - version: v12.19.0
1779    pr-url: https://github.com/nodejs/node/pull/34729
1780    description: This function is also available as `buf.readUint16LE()`.
1781  - version: v10.0.0
1782    pr-url: https://github.com/nodejs/node/pull/18395
1783    description: Removed `noAssert` and no implicit coercion of the offset
1784                 to `uint32` anymore.
1785-->
1786
1787* `offset` {integer} Number of bytes to skip before starting to read. Must
1788  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
1789* Returns: {integer}
1790
1791Reads an unsigned, little-endian 16-bit integer from `buf` at the specified
1792`offset`.
1793
1794```js
1795const buf = Buffer.from([0x12, 0x34, 0x56]);
1796
1797console.log(buf.readUInt16LE(0).toString(16));
1798// Prints: 3412
1799console.log(buf.readUInt16LE(1).toString(16));
1800// Prints: 5634
1801console.log(buf.readUInt16LE(2).toString(16));
1802// Throws ERR_OUT_OF_RANGE.
1803```
1804
1805### `buf.readUInt32BE([offset])`
1806<!-- YAML
1807added: v0.5.5
1808changes:
1809  - version: v12.19.0
1810    pr-url: https://github.com/nodejs/node/pull/34729
1811    description: This function is also available as `buf.readUint32BE()`.
1812  - version: v10.0.0
1813    pr-url: https://github.com/nodejs/node/pull/18395
1814    description: Removed `noAssert` and no implicit coercion of the offset
1815                 to `uint32` anymore.
1816-->
1817
1818* `offset` {integer} Number of bytes to skip before starting to read. Must
1819  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
1820* Returns: {integer}
1821
1822Reads an unsigned, big-endian 32-bit integer from `buf` at the specified
1823`offset`.
1824
1825```js
1826const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
1827
1828console.log(buf.readUInt32BE(0).toString(16));
1829// Prints: 12345678
1830```
1831
1832### `buf.readUInt32LE([offset])`
1833<!-- YAML
1834added: v0.5.5
1835changes:
1836  - version: v12.19.0
1837    pr-url: https://github.com/nodejs/node/pull/34729
1838    description: This function is also available as `buf.readUint32LE()`.
1839  - version: v10.0.0
1840    pr-url: https://github.com/nodejs/node/pull/18395
1841    description: Removed `noAssert` and no implicit coercion of the offset
1842                 to `uint32` anymore.
1843-->
1844
1845* `offset` {integer} Number of bytes to skip before starting to read. Must
1846  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
1847* Returns: {integer}
1848
1849Reads an unsigned, little-endian 32-bit integer from `buf` at the specified
1850`offset`.
1851
1852```js
1853const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
1854
1855console.log(buf.readUInt32LE(0).toString(16));
1856// Prints: 78563412
1857console.log(buf.readUInt32LE(1).toString(16));
1858// Throws ERR_OUT_OF_RANGE.
1859```
1860
1861### `buf.readUIntBE(offset, byteLength)`
1862<!-- YAML
1863added: v0.11.15
1864changes:
1865  - version: v12.19.0
1866    pr-url: https://github.com/nodejs/node/pull/34729
1867    description: This function is also available as `buf.readUintBE()`.
1868  - version: v10.0.0
1869    pr-url: https://github.com/nodejs/node/pull/18395
1870    description: Removed `noAssert` and no implicit coercion of the offset
1871                 and `byteLength` to `uint32` anymore.
1872-->
1873
1874* `offset` {integer} Number of bytes to skip before starting to read. Must
1875  satisfy `0 <= offset <= buf.length - byteLength`.
1876* `byteLength` {integer} Number of bytes to read. Must satisfy
1877  `0 < byteLength <= 6`.
1878* Returns: {integer}
1879
1880Reads `byteLength` number of bytes from `buf` at the specified `offset`
1881and interprets the result as an unsigned big-endian integer supporting
1882up to 48 bits of accuracy.
1883
1884```js
1885const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
1886
1887console.log(buf.readUIntBE(0, 6).toString(16));
1888// Prints: 1234567890ab
1889console.log(buf.readUIntBE(1, 6).toString(16));
1890// Throws ERR_OUT_OF_RANGE.
1891```
1892
1893### `buf.readUIntLE(offset, byteLength)`
1894<!-- YAML
1895added: v0.11.15
1896changes:
1897  - version: v12.19.0
1898    pr-url: https://github.com/nodejs/node/pull/34729
1899    description: This function is also available as `buf.readUintLE()`.
1900  - version: v10.0.0
1901    pr-url: https://github.com/nodejs/node/pull/18395
1902    description: Removed `noAssert` and no implicit coercion of the offset
1903                 and `byteLength` to `uint32` anymore.
1904-->
1905
1906* `offset` {integer} Number of bytes to skip before starting to read. Must
1907  satisfy `0 <= offset <= buf.length - byteLength`.
1908* `byteLength` {integer} Number of bytes to read. Must satisfy
1909  `0 < byteLength <= 6`.
1910* Returns: {integer}
1911
1912Reads `byteLength` number of bytes from `buf` at the specified `offset`
1913and interprets the result as an unsigned, little-endian integer supporting
1914up to 48 bits of accuracy.
1915
1916```js
1917const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
1918
1919console.log(buf.readUIntLE(0, 6).toString(16));
1920// Prints: ab9078563412
1921```
1922
1923### `buf.subarray([start[, end]])`
1924<!-- YAML
1925added: v3.0.0
1926-->
1927
1928* `start` {integer} Where the new `Buffer` will start. **Default:** `0`.
1929* `end` {integer} Where the new `Buffer` will end (not inclusive).
1930  **Default:** [`buf.length`][].
1931* Returns: {Buffer}
1932
1933Returns a new `Buffer` that references the same memory as the original, but
1934offset and cropped by the `start` and `end` indices.
1935
1936Specifying `end` greater than [`buf.length`][] will return the same result as
1937that of `end` equal to [`buf.length`][].
1938
1939This method is inherited from [`TypedArray#subarray()`][].
1940
1941Modifying the new `Buffer` slice will modify the memory in the original `Buffer`
1942because the allocated memory of the two objects overlap.
1943
1944```js
1945// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte
1946// from the original `Buffer`.
1947
1948const buf1 = Buffer.allocUnsafe(26);
1949
1950for (let i = 0; i < 26; i++) {
1951  // 97 is the decimal ASCII value for 'a'.
1952  buf1[i] = i + 97;
1953}
1954
1955const buf2 = buf1.subarray(0, 3);
1956
1957console.log(buf2.toString('ascii', 0, buf2.length));
1958// Prints: abc
1959
1960buf1[0] = 33;
1961
1962console.log(buf2.toString('ascii', 0, buf2.length));
1963// Prints: !bc
1964```
1965
1966Specifying negative indexes causes the slice to be generated relative to the
1967end of `buf` rather than the beginning.
1968
1969```js
1970const buf = Buffer.from('buffer');
1971
1972console.log(buf.subarray(-6, -1).toString());
1973// Prints: buffe
1974// (Equivalent to buf.subarray(0, 5).)
1975
1976console.log(buf.subarray(-6, -2).toString());
1977// Prints: buff
1978// (Equivalent to buf.subarray(0, 4).)
1979
1980console.log(buf.subarray(-5, -2).toString());
1981// Prints: uff
1982// (Equivalent to buf.subarray(1, 4).)
1983```
1984
1985### `buf.slice([start[, end]])`
1986<!-- YAML
1987added: v0.3.0
1988changes:
1989  - version: v7.1.0, v6.9.2
1990    pr-url: https://github.com/nodejs/node/pull/9341
1991    description: Coercing the offsets to integers now handles values outside
1992                 the 32-bit integer range properly.
1993  - version: v7.0.0
1994    pr-url: https://github.com/nodejs/node/pull/9101
1995    description: All offsets are now coerced to integers before doing any
1996                 calculations with them.
1997-->
1998
1999* `start` {integer} Where the new `Buffer` will start. **Default:** `0`.
2000* `end` {integer} Where the new `Buffer` will end (not inclusive).
2001  **Default:** [`buf.length`][].
2002* Returns: {Buffer}
2003
2004Returns a new `Buffer` that references the same memory as the original, but
2005offset and cropped by the `start` and `end` indices.
2006
2007This is the same behavior as `buf.subarray()`.
2008
2009This method is not compatible with the `Uint8Array.prototype.slice()`,
2010which is a superclass of `Buffer`. To copy the slice, use
2011`Uint8Array.prototype.slice()`.
2012
2013```js
2014const buf = Buffer.from('buffer');
2015
2016const copiedBuf = Uint8Array.prototype.slice.call(buf);
2017copiedBuf[0]++;
2018console.log(copiedBuf.toString());
2019// Prints: cuffer
2020
2021console.log(buf.toString());
2022// Prints: buffer
2023```
2024
2025### `buf.swap16()`
2026<!-- YAML
2027added: v5.10.0
2028-->
2029
2030* Returns: {Buffer} A reference to `buf`.
2031
2032Interprets `buf` as an array of unsigned 16-bit integers and swaps the
2033byte order *in-place*. Throws [`ERR_INVALID_BUFFER_SIZE`][] if [`buf.length`][]
2034is not a multiple of 2.
2035
2036```js
2037const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
2038
2039console.log(buf1);
2040// Prints: <Buffer 01 02 03 04 05 06 07 08>
2041
2042buf1.swap16();
2043
2044console.log(buf1);
2045// Prints: <Buffer 02 01 04 03 06 05 08 07>
2046
2047const buf2 = Buffer.from([0x1, 0x2, 0x3]);
2048
2049buf2.swap16();
2050// Throws ERR_INVALID_BUFFER_SIZE.
2051```
2052
2053One convenient use of `buf.swap16()` is to perform a fast in-place conversion
2054between UTF-16 little-endian and UTF-16 big-endian:
2055
2056```js
2057const buf = Buffer.from('This is little-endian UTF-16', 'utf16le');
2058buf.swap16(); // Convert to big-endian UTF-16 text.
2059```
2060
2061### `buf.swap32()`
2062<!-- YAML
2063added: v5.10.0
2064-->
2065
2066* Returns: {Buffer} A reference to `buf`.
2067
2068Interprets `buf` as an array of unsigned 32-bit integers and swaps the
2069byte order *in-place*. Throws [`ERR_INVALID_BUFFER_SIZE`][] if [`buf.length`][]
2070is not a multiple of 4.
2071
2072```js
2073const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
2074
2075console.log(buf1);
2076// Prints: <Buffer 01 02 03 04 05 06 07 08>
2077
2078buf1.swap32();
2079
2080console.log(buf1);
2081// Prints: <Buffer 04 03 02 01 08 07 06 05>
2082
2083const buf2 = Buffer.from([0x1, 0x2, 0x3]);
2084
2085buf2.swap32();
2086// Throws ERR_INVALID_BUFFER_SIZE.
2087```
2088
2089### `buf.swap64()`
2090<!-- YAML
2091added: v6.3.0
2092-->
2093
2094* Returns: {Buffer} A reference to `buf`.
2095
2096Interprets `buf` as an array of 64-bit numbers and swaps byte order *in-place*.
2097Throws [`ERR_INVALID_BUFFER_SIZE`][] if [`buf.length`][] is not a multiple of 8.
2098
2099```js
2100const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
2101
2102console.log(buf1);
2103// Prints: <Buffer 01 02 03 04 05 06 07 08>
2104
2105buf1.swap64();
2106
2107console.log(buf1);
2108// Prints: <Buffer 08 07 06 05 04 03 02 01>
2109
2110const buf2 = Buffer.from([0x1, 0x2, 0x3]);
2111
2112buf2.swap64();
2113// Throws ERR_INVALID_BUFFER_SIZE.
2114```
2115
2116### `buf.toJSON()`
2117<!-- YAML
2118added: v0.9.2
2119-->
2120
2121* Returns: {Object}
2122
2123Returns a JSON representation of `buf`. [`JSON.stringify()`][] implicitly calls
2124this function when stringifying a `Buffer` instance.
2125
2126`Buffer.from()` accepts objects in the format returned from this method.
2127In particular, `Buffer.from(buf.toJSON())` works like `Buffer.from(buf)`.
2128
2129```js
2130const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
2131const json = JSON.stringify(buf);
2132
2133console.log(json);
2134// Prints: {"type":"Buffer","data":[1,2,3,4,5]}
2135
2136const copy = JSON.parse(json, (key, value) => {
2137  return value && value.type === 'Buffer' ?
2138    Buffer.from(value) :
2139    value;
2140});
2141
2142console.log(copy);
2143// Prints: <Buffer 01 02 03 04 05>
2144```
2145
2146### `buf.toString([encoding[, start[, end]]])`
2147<!-- YAML
2148added: v0.1.90
2149-->
2150
2151* `encoding` {string} The character encoding to use. **Default:** `'utf8'`.
2152* `start` {integer} The byte offset to start decoding at. **Default:** `0`.
2153* `end` {integer} The byte offset to stop decoding at (not inclusive).
2154  **Default:** [`buf.length`][].
2155* Returns: {string}
2156
2157Decodes `buf` to a string according to the specified character encoding in
2158`encoding`. `start` and `end` may be passed to decode only a subset of `buf`.
2159
2160If `encoding` is `'utf8'` and a byte sequence in the input is not valid UTF-8,
2161then each invalid byte is replaced with the replacement character `U+FFFD`.
2162
2163The maximum length of a string instance (in UTF-16 code units) is available
2164as [`buffer.constants.MAX_STRING_LENGTH`][].
2165
2166```js
2167const buf1 = Buffer.allocUnsafe(26);
2168
2169for (let i = 0; i < 26; i++) {
2170  // 97 is the decimal ASCII value for 'a'.
2171  buf1[i] = i + 97;
2172}
2173
2174console.log(buf1.toString('utf8'));
2175// Prints: abcdefghijklmnopqrstuvwxyz
2176console.log(buf1.toString('utf8', 0, 5));
2177// Prints: abcde
2178
2179const buf2 = Buffer.from('tést');
2180
2181console.log(buf2.toString('hex'));
2182// Prints: 74c3a97374
2183console.log(buf2.toString('utf8', 0, 3));
2184// Prints: té
2185console.log(buf2.toString(undefined, 0, 3));
2186// Prints: té
2187```
2188
2189### `buf.values()`
2190<!-- YAML
2191added: v1.1.0
2192-->
2193
2194* Returns: {Iterator}
2195
2196Creates and returns an [iterator][] for `buf` values (bytes). This function is
2197called automatically when a `Buffer` is used in a `for..of` statement.
2198
2199```js
2200const buf = Buffer.from('buffer');
2201
2202for (const value of buf.values()) {
2203  console.log(value);
2204}
2205// Prints:
2206//   98
2207//   117
2208//   102
2209//   102
2210//   101
2211//   114
2212
2213for (const value of buf) {
2214  console.log(value);
2215}
2216// Prints:
2217//   98
2218//   117
2219//   102
2220//   102
2221//   101
2222//   114
2223```
2224
2225### `buf.write(string[, offset[, length]][, encoding])`
2226<!-- YAML
2227added: v0.1.90
2228-->
2229
2230* `string` {string} String to write to `buf`.
2231* `offset` {integer} Number of bytes to skip before starting to write `string`.
2232  **Default:** `0`.
2233* `length` {integer} Maximum number of bytes to write (written bytes will not
2234  exceed `buf.length - offset`). **Default:** `buf.length - offset`.
2235* `encoding` {string} The character encoding of `string`. **Default:** `'utf8'`.
2236* Returns: {integer} Number of bytes written.
2237
2238Writes `string` to `buf` at `offset` according to the character encoding in
2239`encoding`. The `length` parameter is the number of bytes to write. If `buf` did
2240not contain enough space to fit the entire string, only part of `string` will be
2241written. However, partially encoded characters will not be written.
2242
2243```js
2244const buf = Buffer.alloc(256);
2245
2246const len = buf.write('\u00bd + \u00bc = \u00be', 0);
2247
2248console.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);
2249// Prints: 12 bytes: ½ + ¼ = ¾
2250
2251const buffer = Buffer.alloc(10);
2252
2253const length = buffer.write('abcd', 8);
2254
2255console.log(`${length} bytes: ${buffer.toString('utf8', 8, 10)}`);
2256// Prints: 2 bytes : ab
2257```
2258
2259### `buf.writeBigInt64BE(value[, offset])`
2260<!-- YAML
2261added: v12.0.0
2262-->
2263
2264* `value` {bigint} Number to be written to `buf`.
2265* `offset` {integer} Number of bytes to skip before starting to write. Must
2266  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
2267* Returns: {integer} `offset` plus the number of bytes written.
2268
2269Writes `value` to `buf` at the specified `offset` as big-endian.
2270
2271`value` is interpreted and written as a two's complement signed integer.
2272
2273```js
2274const buf = Buffer.allocUnsafe(8);
2275
2276buf.writeBigInt64BE(0x0102030405060708n, 0);
2277
2278console.log(buf);
2279// Prints: <Buffer 01 02 03 04 05 06 07 08>
2280```
2281
2282### `buf.writeBigInt64LE(value[, offset])`
2283<!-- YAML
2284added:
2285 - v12.0.0
2286 - v10.20.0
2287-->
2288
2289* `value` {bigint} Number to be written to `buf`.
2290* `offset` {integer} Number of bytes to skip before starting to write. Must
2291  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
2292* Returns: {integer} `offset` plus the number of bytes written.
2293
2294Writes `value` to `buf` at the specified `offset` as little-endian.
2295
2296`value` is interpreted and written as a two's complement signed integer.
2297
2298```js
2299const buf = Buffer.allocUnsafe(8);
2300
2301buf.writeBigInt64LE(0x0102030405060708n, 0);
2302
2303console.log(buf);
2304// Prints: <Buffer 08 07 06 05 04 03 02 01>
2305```
2306
2307### `buf.writeBigUInt64BE(value[, offset])`
2308<!-- YAML
2309added:
2310 - v12.0.0
2311 - v10.20.0
2312changes:
2313  - version: v12.19.0
2314    pr-url: https://github.com/nodejs/node/pull/34960
2315    description: This function is also available as `buf.writeBigUint64BE()`.
2316-->
2317
2318* `value` {bigint} Number to be written to `buf`.
2319* `offset` {integer} Number of bytes to skip before starting to write. Must
2320  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
2321* Returns: {integer} `offset` plus the number of bytes written.
2322
2323Writes `value` to `buf` at the specified `offset` as big-endian.
2324
2325```js
2326const buf = Buffer.allocUnsafe(8);
2327
2328buf.writeBigUInt64BE(0xdecafafecacefaden, 0);
2329
2330console.log(buf);
2331// Prints: <Buffer de ca fa fe ca ce fa de>
2332```
2333
2334### `buf.writeBigUInt64LE(value[, offset])`
2335<!-- YAML
2336added: v12.0.0
2337changes:
2338  - version: v12.19.0
2339    pr-url: https://github.com/nodejs/node/pull/34960
2340    description: This function is also available as `buf.writeBigUint64LE()`.
2341-->
2342
2343* `value` {bigint} Number to be written to `buf`.
2344* `offset` {integer} Number of bytes to skip before starting to write. Must
2345  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
2346* Returns: {integer} `offset` plus the number of bytes written.
2347
2348Writes `value` to `buf` at the specified `offset` as little-endian
2349
2350```js
2351const buf = Buffer.allocUnsafe(8);
2352
2353buf.writeBigUInt64LE(0xdecafafecacefaden, 0);
2354
2355console.log(buf);
2356// Prints: <Buffer de fa ce ca fe fa ca de>
2357```
2358
2359### `buf.writeDoubleBE(value[, offset])`
2360<!-- YAML
2361added: v0.11.15
2362changes:
2363  - version: v10.0.0
2364    pr-url: https://github.com/nodejs/node/pull/18395
2365    description: Removed `noAssert` and no implicit coercion of the offset
2366                 to `uint32` anymore.
2367-->
2368
2369* `value` {number} Number to be written to `buf`.
2370* `offset` {integer} Number of bytes to skip before starting to write. Must
2371  satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.
2372* Returns: {integer} `offset` plus the number of bytes written.
2373
2374Writes `value` to `buf` at the specified `offset` as big-endian. The `value`
2375must be a JavaScript number. Behavior is undefined when `value` is anything
2376other than a JavaScript number.
2377
2378```js
2379const buf = Buffer.allocUnsafe(8);
2380
2381buf.writeDoubleBE(123.456, 0);
2382
2383console.log(buf);
2384// Prints: <Buffer 40 5e dd 2f 1a 9f be 77>
2385```
2386
2387### `buf.writeDoubleLE(value[, offset])`
2388<!-- YAML
2389added: v0.11.15
2390changes:
2391  - version: v10.0.0
2392    pr-url: https://github.com/nodejs/node/pull/18395
2393    description: Removed `noAssert` and no implicit coercion of the offset
2394                 to `uint32` anymore.
2395-->
2396
2397* `value` {number} Number to be written to `buf`.
2398* `offset` {integer} Number of bytes to skip before starting to write. Must
2399  satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.
2400* Returns: {integer} `offset` plus the number of bytes written.
2401
2402Writes `value` to `buf` at the specified `offset` as little-endian. The `value`
2403must be a JavaScript number. Behavior is undefined when `value` is anything
2404other than a JavaScript number.
2405
2406```js
2407const buf = Buffer.allocUnsafe(8);
2408
2409buf.writeDoubleLE(123.456, 0);
2410
2411console.log(buf);
2412// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40>
2413```
2414
2415### `buf.writeFloatBE(value[, offset])`
2416<!-- YAML
2417added: v0.11.15
2418changes:
2419  - version: v10.0.0
2420    pr-url: https://github.com/nodejs/node/pull/18395
2421    description: Removed `noAssert` and no implicit coercion of the offset
2422                 to `uint32` anymore.
2423-->
2424
2425* `value` {number} Number to be written to `buf`.
2426* `offset` {integer} Number of bytes to skip before starting to write. Must
2427  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
2428* Returns: {integer} `offset` plus the number of bytes written.
2429
2430Writes `value` to `buf` at the specified `offset` as big-endian. Behavior is
2431undefined when `value` is anything other than a JavaScript number.
2432
2433```js
2434const buf = Buffer.allocUnsafe(4);
2435
2436buf.writeFloatBE(0xcafebabe, 0);
2437
2438console.log(buf);
2439// Prints: <Buffer 4f 4a fe bb>
2440
2441```
2442
2443### `buf.writeFloatLE(value[, offset])`
2444<!-- YAML
2445added: v0.11.15
2446changes:
2447  - version: v10.0.0
2448    pr-url: https://github.com/nodejs/node/pull/18395
2449    description: Removed `noAssert` and no implicit coercion of the offset
2450                 to `uint32` anymore.
2451-->
2452
2453* `value` {number} Number to be written to `buf`.
2454* `offset` {integer} Number of bytes to skip before starting to write. Must
2455  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
2456* Returns: {integer} `offset` plus the number of bytes written.
2457
2458Writes `value` to `buf` at the specified `offset` as little-endian. Behavior is
2459undefined when `value` is anything other than a JavaScript number.
2460
2461```js
2462const buf = Buffer.allocUnsafe(4);
2463
2464buf.writeFloatLE(0xcafebabe, 0);
2465
2466console.log(buf);
2467// Prints: <Buffer bb fe 4a 4f>
2468```
2469
2470### `buf.writeInt8(value[, offset])`
2471<!-- YAML
2472added: v0.5.0
2473changes:
2474  - version: v10.0.0
2475    pr-url: https://github.com/nodejs/node/pull/18395
2476    description: Removed `noAssert` and no implicit coercion of the offset
2477                 to `uint32` anymore.
2478-->
2479
2480* `value` {integer} Number to be written to `buf`.
2481* `offset` {integer} Number of bytes to skip before starting to write. Must
2482  satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.
2483* Returns: {integer} `offset` plus the number of bytes written.
2484
2485Writes `value` to `buf` at the specified `offset`. `value` must be a valid
2486signed 8-bit integer. Behavior is undefined when `value` is anything other than
2487a signed 8-bit integer.
2488
2489`value` is interpreted and written as a two's complement signed integer.
2490
2491```js
2492const buf = Buffer.allocUnsafe(2);
2493
2494buf.writeInt8(2, 0);
2495buf.writeInt8(-2, 1);
2496
2497console.log(buf);
2498// Prints: <Buffer 02 fe>
2499```
2500
2501### `buf.writeInt16BE(value[, offset])`
2502<!-- YAML
2503added: v0.5.5
2504changes:
2505  - version: v10.0.0
2506    pr-url: https://github.com/nodejs/node/pull/18395
2507    description: Removed `noAssert` and no implicit coercion of the offset
2508                 to `uint32` anymore.
2509-->
2510
2511* `value` {integer} Number to be written to `buf`.
2512* `offset` {integer} Number of bytes to skip before starting to write. Must
2513  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
2514* Returns: {integer} `offset` plus the number of bytes written.
2515
2516Writes `value` to `buf` at the specified `offset` as big-endian.  The `value`
2517must be a valid signed 16-bit integer. Behavior is undefined when `value` is
2518anything other than a signed 16-bit integer.
2519
2520The `value` is interpreted and written as a two's complement signed integer.
2521
2522```js
2523const buf = Buffer.allocUnsafe(2);
2524
2525buf.writeInt16BE(0x0102, 0);
2526
2527console.log(buf);
2528// Prints: <Buffer 01 02>
2529```
2530
2531### `buf.writeInt16LE(value[, offset])`
2532<!-- YAML
2533added: v0.5.5
2534changes:
2535  - version: v10.0.0
2536    pr-url: https://github.com/nodejs/node/pull/18395
2537    description: Removed `noAssert` and no implicit coercion of the offset
2538                 to `uint32` anymore.
2539-->
2540
2541* `value` {integer} Number to be written to `buf`.
2542* `offset` {integer} Number of bytes to skip before starting to write. Must
2543  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
2544* Returns: {integer} `offset` plus the number of bytes written.
2545
2546Writes `value` to `buf` at the specified `offset` as little-endian.  The `value`
2547must be a valid signed 16-bit integer. Behavior is undefined when `value` is
2548anything other than a signed 16-bit integer.
2549
2550The `value` is interpreted and written as a two's complement signed integer.
2551
2552```js
2553const buf = Buffer.allocUnsafe(2);
2554
2555buf.writeInt16LE(0x0304, 0);
2556
2557console.log(buf);
2558// Prints: <Buffer 04 03>
2559```
2560
2561### `buf.writeInt32BE(value[, offset])`
2562<!-- YAML
2563added: v0.5.5
2564changes:
2565  - version: v10.0.0
2566    pr-url: https://github.com/nodejs/node/pull/18395
2567    description: Removed `noAssert` and no implicit coercion of the offset
2568                 to `uint32` anymore.
2569-->
2570
2571* `value` {integer} Number to be written to `buf`.
2572* `offset` {integer} Number of bytes to skip before starting to write. Must
2573  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
2574* Returns: {integer} `offset` plus the number of bytes written.
2575
2576Writes `value` to `buf` at the specified `offset` as big-endian. The `value`
2577must be a valid signed 32-bit integer. Behavior is undefined when `value` is
2578anything other than a signed 32-bit integer.
2579
2580The `value` is interpreted and written as a two's complement signed integer.
2581
2582```js
2583const buf = Buffer.allocUnsafe(4);
2584
2585buf.writeInt32BE(0x01020304, 0);
2586
2587console.log(buf);
2588// Prints: <Buffer 01 02 03 04>
2589```
2590
2591### `buf.writeInt32LE(value[, offset])`
2592<!-- YAML
2593added: v0.5.5
2594changes:
2595  - version: v10.0.0
2596    pr-url: https://github.com/nodejs/node/pull/18395
2597    description: Removed `noAssert` and no implicit coercion of the offset
2598                 to `uint32` anymore.
2599-->
2600
2601* `value` {integer} Number to be written to `buf`.
2602* `offset` {integer} Number of bytes to skip before starting to write. Must
2603  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
2604* Returns: {integer} `offset` plus the number of bytes written.
2605
2606Writes `value` to `buf` at the specified `offset` as little-endian. The `value`
2607must be a valid signed 32-bit integer. Behavior is undefined when `value` is
2608anything other than a signed 32-bit integer.
2609
2610The `value` is interpreted and written as a two's complement signed integer.
2611
2612```js
2613const buf = Buffer.allocUnsafe(4);
2614
2615buf.writeInt32LE(0x05060708, 0);
2616
2617console.log(buf);
2618// Prints: <Buffer 08 07 06 05>
2619```
2620
2621### `buf.writeIntBE(value, offset, byteLength)`
2622<!-- YAML
2623added: v0.11.15
2624changes:
2625  - version: v10.0.0
2626    pr-url: https://github.com/nodejs/node/pull/18395
2627    description: Removed `noAssert` and no implicit coercion of the offset
2628                 and `byteLength` to `uint32` anymore.
2629-->
2630
2631* `value` {integer} Number to be written to `buf`.
2632* `offset` {integer} Number of bytes to skip before starting to write. Must
2633  satisfy `0 <= offset <= buf.length - byteLength`.
2634* `byteLength` {integer} Number of bytes to write. Must satisfy
2635  `0 < byteLength <= 6`.
2636* Returns: {integer} `offset` plus the number of bytes written.
2637
2638Writes `byteLength` bytes of `value` to `buf` at the specified `offset`
2639as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when
2640`value` is anything other than a signed integer.
2641
2642```js
2643const buf = Buffer.allocUnsafe(6);
2644
2645buf.writeIntBE(0x1234567890ab, 0, 6);
2646
2647console.log(buf);
2648// Prints: <Buffer 12 34 56 78 90 ab>
2649
2650```
2651
2652### `buf.writeIntLE(value, offset, byteLength)`
2653<!-- YAML
2654added: v0.11.15
2655changes:
2656  - version: v10.0.0
2657    pr-url: https://github.com/nodejs/node/pull/18395
2658    description: Removed `noAssert` and no implicit coercion of the offset
2659                 and `byteLength` to `uint32` anymore.
2660-->
2661
2662* `value` {integer} Number to be written to `buf`.
2663* `offset` {integer} Number of bytes to skip before starting to write. Must
2664  satisfy `0 <= offset <= buf.length - byteLength`.
2665* `byteLength` {integer} Number of bytes to write. Must satisfy
2666  `0 < byteLength <= 6`.
2667* Returns: {integer} `offset` plus the number of bytes written.
2668
2669Writes `byteLength` bytes of `value` to `buf` at the specified `offset`
2670as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
2671when `value` is anything other than a signed integer.
2672
2673```js
2674const buf = Buffer.allocUnsafe(6);
2675
2676buf.writeIntLE(0x1234567890ab, 0, 6);
2677
2678console.log(buf);
2679// Prints: <Buffer ab 90 78 56 34 12>
2680```
2681
2682### `buf.writeUInt8(value[, offset])`
2683<!-- YAML
2684added: v0.5.0
2685changes:
2686  - version: v12.19.0
2687    pr-url: https://github.com/nodejs/node/pull/34729
2688    description: This function is also available as `buf.writeUint8()`.
2689  - version: v10.0.0
2690    pr-url: https://github.com/nodejs/node/pull/18395
2691    description: Removed `noAssert` and no implicit coercion of the offset
2692                 to `uint32` anymore.
2693-->
2694
2695* `value` {integer} Number to be written to `buf`.
2696* `offset` {integer} Number of bytes to skip before starting to write. Must
2697  satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.
2698* Returns: {integer} `offset` plus the number of bytes written.
2699
2700Writes `value` to `buf` at the specified `offset`. `value` must be a
2701valid unsigned 8-bit integer. Behavior is undefined when `value` is anything
2702other than an unsigned 8-bit integer.
2703
2704```js
2705const buf = Buffer.allocUnsafe(4);
2706
2707buf.writeUInt8(0x3, 0);
2708buf.writeUInt8(0x4, 1);
2709buf.writeUInt8(0x23, 2);
2710buf.writeUInt8(0x42, 3);
2711
2712console.log(buf);
2713// Prints: <Buffer 03 04 23 42>
2714```
2715
2716### `buf.writeUInt16BE(value[, offset])`
2717<!-- YAML
2718added: v0.5.5
2719changes:
2720  - version: v12.19.0
2721    pr-url: https://github.com/nodejs/node/pull/34729
2722    description: This function is also available as `buf.writeUint16BE()`.
2723  - version: v10.0.0
2724    pr-url: https://github.com/nodejs/node/pull/18395
2725    description: Removed `noAssert` and no implicit coercion of the offset
2726                 to `uint32` anymore.
2727-->
2728
2729* `value` {integer} Number to be written to `buf`.
2730* `offset` {integer} Number of bytes to skip before starting to write. Must
2731  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
2732* Returns: {integer} `offset` plus the number of bytes written.
2733
2734Writes `value` to `buf` at the specified `offset` as big-endian. The `value`
2735must be a valid unsigned 16-bit integer. Behavior is undefined when `value`
2736is anything other than an unsigned 16-bit integer.
2737
2738```js
2739const buf = Buffer.allocUnsafe(4);
2740
2741buf.writeUInt16BE(0xdead, 0);
2742buf.writeUInt16BE(0xbeef, 2);
2743
2744console.log(buf);
2745// Prints: <Buffer de ad be ef>
2746```
2747
2748### `buf.writeUInt16LE(value[, offset])`
2749<!-- YAML
2750added: v0.5.5
2751changes:
2752  - version: v12.19.0
2753    pr-url: https://github.com/nodejs/node/pull/34729
2754    description: This function is also available as `buf.writeUint16LE()`.
2755  - version: v10.0.0
2756    pr-url: https://github.com/nodejs/node/pull/18395
2757    description: Removed `noAssert` and no implicit coercion of the offset
2758                 to `uint32` anymore.
2759-->
2760
2761* `value` {integer} Number to be written to `buf`.
2762* `offset` {integer} Number of bytes to skip before starting to write. Must
2763  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
2764* Returns: {integer} `offset` plus the number of bytes written.
2765
2766Writes `value` to `buf` at the specified `offset` as little-endian. The `value`
2767must be a valid unsigned 16-bit integer. Behavior is undefined when `value` is
2768anything other than an unsigned 16-bit integer.
2769
2770```js
2771const buf = Buffer.allocUnsafe(4);
2772
2773buf.writeUInt16LE(0xdead, 0);
2774buf.writeUInt16LE(0xbeef, 2);
2775
2776console.log(buf);
2777// Prints: <Buffer ad de ef be>
2778```
2779
2780### `buf.writeUInt32BE(value[, offset])`
2781<!-- YAML
2782added: v0.5.5
2783changes:
2784  - version: v12.19.0
2785    pr-url: https://github.com/nodejs/node/pull/34729
2786    description: This function is also available as `buf.writeUint32BE()`.
2787  - version: v10.0.0
2788    pr-url: https://github.com/nodejs/node/pull/18395
2789    description: Removed `noAssert` and no implicit coercion of the offset
2790                 to `uint32` anymore.
2791-->
2792
2793* `value` {integer} Number to be written to `buf`.
2794* `offset` {integer} Number of bytes to skip before starting to write. Must
2795  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
2796* Returns: {integer} `offset` plus the number of bytes written.
2797
2798Writes `value` to `buf` at the specified `offset` as big-endian. The `value`
2799must be a valid unsigned 32-bit integer. Behavior is undefined when `value`
2800is anything other than an unsigned 32-bit integer.
2801
2802```js
2803const buf = Buffer.allocUnsafe(4);
2804
2805buf.writeUInt32BE(0xfeedface, 0);
2806
2807console.log(buf);
2808// Prints: <Buffer fe ed fa ce>
2809```
2810
2811### `buf.writeUInt32LE(value[, offset])`
2812<!-- YAML
2813added: v0.5.5
2814changes:
2815  - version: v12.19.0
2816    pr-url: https://github.com/nodejs/node/pull/34729
2817    description: This function is also available as `buf.writeUint32LE()`.
2818  - version: v10.0.0
2819    pr-url: https://github.com/nodejs/node/pull/18395
2820    description: Removed `noAssert` and no implicit coercion of the offset
2821                 to `uint32` anymore.
2822-->
2823
2824* `value` {integer} Number to be written to `buf`.
2825* `offset` {integer} Number of bytes to skip before starting to write. Must
2826  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
2827* Returns: {integer} `offset` plus the number of bytes written.
2828
2829Writes `value` to `buf` at the specified `offset` as little-endian. The `value`
2830must be a valid unsigned 32-bit integer. Behavior is undefined when `value` is
2831anything other than an unsigned 32-bit integer.
2832
2833```js
2834const buf = Buffer.allocUnsafe(4);
2835
2836buf.writeUInt32LE(0xfeedface, 0);
2837
2838console.log(buf);
2839// Prints: <Buffer ce fa ed fe>
2840```
2841
2842### `buf.writeUIntBE(value, offset, byteLength)`
2843<!-- YAML
2844added: v0.5.5
2845changes:
2846  - version: v12.19.0
2847    pr-url: https://github.com/nodejs/node/pull/34729
2848    description: This function is also available as `buf.writeUintBE()`.
2849  - version: v10.0.0
2850    pr-url: https://github.com/nodejs/node/pull/18395
2851    description: Removed `noAssert` and no implicit coercion of the offset
2852                 and `byteLength` to `uint32` anymore.
2853-->
2854
2855* `value` {integer} Number to be written to `buf`.
2856* `offset` {integer} Number of bytes to skip before starting to write. Must
2857  satisfy `0 <= offset <= buf.length - byteLength`.
2858* `byteLength` {integer} Number of bytes to write. Must satisfy
2859  `0 < byteLength <= 6`.
2860* Returns: {integer} `offset` plus the number of bytes written.
2861
2862Writes `byteLength` bytes of `value` to `buf` at the specified `offset`
2863as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined
2864when `value` is anything other than an unsigned integer.
2865
2866```js
2867const buf = Buffer.allocUnsafe(6);
2868
2869buf.writeUIntBE(0x1234567890ab, 0, 6);
2870
2871console.log(buf);
2872// Prints: <Buffer 12 34 56 78 90 ab>
2873```
2874
2875### `buf.writeUIntLE(value, offset, byteLength)`
2876<!-- YAML
2877added: v0.5.5
2878changes:
2879  - version: v12.19.0
2880    pr-url: https://github.com/nodejs/node/pull/34729
2881    description: This function is also available as `buf.writeUintLE()`.
2882  - version: v10.0.0
2883    pr-url: https://github.com/nodejs/node/pull/18395
2884    description: Removed `noAssert` and no implicit coercion of the offset
2885                 and `byteLength` to `uint32` anymore.
2886-->
2887
2888* `value` {integer} Number to be written to `buf`.
2889* `offset` {integer} Number of bytes to skip before starting to write. Must
2890  satisfy `0 <= offset <= buf.length - byteLength`.
2891* `byteLength` {integer} Number of bytes to write. Must satisfy
2892  `0 < byteLength <= 6`.
2893* Returns: {integer} `offset` plus the number of bytes written.
2894
2895Writes `byteLength` bytes of `value` to `buf` at the specified `offset`
2896as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
2897when `value` is anything other than an unsigned integer.
2898
2899```js
2900const buf = Buffer.allocUnsafe(6);
2901
2902buf.writeUIntLE(0x1234567890ab, 0, 6);
2903
2904console.log(buf);
2905// Prints: <Buffer ab 90 78 56 34 12>
2906```
2907
2908### `new Buffer(array)`
2909<!-- YAML
2910deprecated: v6.0.0
2911changes:
2912  - version: v10.0.0
2913    pr-url: https://github.com/nodejs/node/pull/19524
2914    description: Calling this constructor emits a deprecation warning when
2915                 run from code outside the `node_modules` directory.
2916  - version: v7.2.1
2917    pr-url: https://github.com/nodejs/node/pull/9529
2918    description: Calling this constructor no longer emits a deprecation warning.
2919  - version: v7.0.0
2920    pr-url: https://github.com/nodejs/node/pull/8169
2921    description: Calling this constructor emits a deprecation warning now.
2922-->
2923
2924> Stability: 0 - Deprecated: Use [`Buffer.from(array)`][] instead.
2925
2926* `array` {integer[]} An array of bytes to copy from.
2927
2928See [`Buffer.from(array)`][].
2929
2930### `new Buffer(arrayBuffer[, byteOffset[, length]])`
2931<!-- YAML
2932added: v3.0.0
2933deprecated: v6.0.0
2934changes:
2935  - version: v10.0.0
2936    pr-url: https://github.com/nodejs/node/pull/19524
2937    description: Calling this constructor emits a deprecation warning when
2938                 run from code outside the `node_modules` directory.
2939  - version: v7.2.1
2940    pr-url: https://github.com/nodejs/node/pull/9529
2941    description: Calling this constructor no longer emits a deprecation warning.
2942  - version: v7.0.0
2943    pr-url: https://github.com/nodejs/node/pull/8169
2944    description: Calling this constructor emits a deprecation warning now.
2945  - version: v6.0.0
2946    pr-url: https://github.com/nodejs/node/pull/4682
2947    description: The `byteOffset` and `length` parameters are supported now.
2948-->
2949
2950> Stability: 0 - Deprecated: Use
2951> [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`]
2952> instead.
2953
2954* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`][],
2955  [`SharedArrayBuffer`][] or the `.buffer` property of a [`TypedArray`][].
2956* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`.
2957* `length` {integer} Number of bytes to expose.
2958  **Default:** `arrayBuffer.byteLength - byteOffset`.
2959
2960See
2961[`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`].
2962
2963### `new Buffer(buffer)`
2964<!-- YAML
2965deprecated: v6.0.0
2966changes:
2967  - version: v10.0.0
2968    pr-url: https://github.com/nodejs/node/pull/19524
2969    description: Calling this constructor emits a deprecation warning when
2970                 run from code outside the `node_modules` directory.
2971  - version: v7.2.1
2972    pr-url: https://github.com/nodejs/node/pull/9529
2973    description: Calling this constructor no longer emits a deprecation warning.
2974  - version: v7.0.0
2975    pr-url: https://github.com/nodejs/node/pull/8169
2976    description: Calling this constructor emits a deprecation warning now.
2977-->
2978
2979> Stability: 0 - Deprecated: Use [`Buffer.from(buffer)`][] instead.
2980
2981* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`][] from
2982  which to copy data.
2983
2984See [`Buffer.from(buffer)`][].
2985
2986### `new Buffer(size)`
2987<!-- YAML
2988deprecated: v6.0.0
2989changes:
2990  - version: v10.0.0
2991    pr-url: https://github.com/nodejs/node/pull/19524
2992    description: Calling this constructor emits a deprecation warning when
2993                 run from code outside the `node_modules` directory.
2994  - version: v8.0.0
2995    pr-url: https://github.com/nodejs/node/pull/12141
2996    description: The `new Buffer(size)` will return zero-filled memory by
2997                 default.
2998  - version: v7.2.1
2999    pr-url: https://github.com/nodejs/node/pull/9529
3000    description: Calling this constructor no longer emits a deprecation warning.
3001  - version: v7.0.0
3002    pr-url: https://github.com/nodejs/node/pull/8169
3003    description: Calling this constructor emits a deprecation warning now.
3004-->
3005
3006> Stability: 0 - Deprecated: Use [`Buffer.alloc()`][] instead (also see
3007> [`Buffer.allocUnsafe()`][]).
3008
3009* `size` {integer} The desired length of the new `Buffer`.
3010
3011See [`Buffer.alloc()`][] and [`Buffer.allocUnsafe()`][]. This variant of the
3012constructor is equivalent to [`Buffer.alloc()`][].
3013
3014### `new Buffer(string[, encoding])`
3015<!-- YAML
3016deprecated: v6.0.0
3017changes:
3018  - version: v10.0.0
3019    pr-url: https://github.com/nodejs/node/pull/19524
3020    description: Calling this constructor emits a deprecation warning when
3021                 run from code outside the `node_modules` directory.
3022  - version: v7.2.1
3023    pr-url: https://github.com/nodejs/node/pull/9529
3024    description: Calling this constructor no longer emits a deprecation warning.
3025  - version: v7.0.0
3026    pr-url: https://github.com/nodejs/node/pull/8169
3027    description: Calling this constructor emits a deprecation warning now.
3028-->
3029
3030> Stability: 0 - Deprecated:
3031> Use [`Buffer.from(string[, encoding])`][`Buffer.from(string)`] instead.
3032
3033* `string` {string} String to encode.
3034* `encoding` {string} The encoding of `string`. **Default:** `'utf8'`.
3035
3036See [`Buffer.from(string[, encoding])`][`Buffer.from(string)`].
3037
3038## `buffer` module APIs
3039
3040While, the `Buffer` object is available as a global, there are additional
3041`Buffer`-related APIs that are available only via the `buffer` module
3042accessed using `require('buffer')`.
3043
3044### `buffer.INSPECT_MAX_BYTES`
3045<!-- YAML
3046added: v0.5.4
3047-->
3048
3049* {integer} **Default:** `50`
3050
3051Returns the maximum number of bytes that will be returned when
3052`buf.inspect()` is called. This can be overridden by user modules. See
3053[`util.inspect()`][] for more details on `buf.inspect()` behavior.
3054
3055### `buffer.kMaxLength`
3056<!-- YAML
3057added: v3.0.0
3058-->
3059
3060* {integer} The largest size allowed for a single `Buffer` instance.
3061
3062An alias for [`buffer.constants.MAX_LENGTH`][].
3063
3064### `buffer.transcode(source, fromEnc, toEnc)`
3065<!-- YAML
3066added: v7.1.0
3067changes:
3068  - version: v8.0.0
3069    pr-url: https://github.com/nodejs/node/pull/10236
3070    description: The `source` parameter can now be a `Uint8Array`.
3071-->
3072
3073* `source` {Buffer|Uint8Array} A `Buffer` or `Uint8Array` instance.
3074* `fromEnc` {string} The current encoding.
3075* `toEnc` {string} To target encoding.
3076* Returns: {Buffer}
3077
3078Re-encodes the given `Buffer` or `Uint8Array` instance from one character
3079encoding to another. Returns a new `Buffer` instance.
3080
3081Throws if the `fromEnc` or `toEnc` specify invalid character encodings or if
3082conversion from `fromEnc` to `toEnc` is not permitted.
3083
3084Encodings supported by `buffer.transcode()` are: `'ascii'`, `'utf8'`,
3085`'utf16le'`, `'ucs2'`, `'latin1'`, and `'binary'`.
3086
3087The transcoding process will use substitution characters if a given byte
3088sequence cannot be adequately represented in the target encoding. For instance:
3089
3090```js
3091const buffer = require('buffer');
3092
3093const newBuf = buffer.transcode(Buffer.from('€'), 'utf8', 'ascii');
3094console.log(newBuf.toString('ascii'));
3095// Prints: '?'
3096```
3097
3098Because the Euro (`€`) sign is not representable in US-ASCII, it is replaced
3099with `?` in the transcoded `Buffer`.
3100
3101### Class: `SlowBuffer`
3102<!-- YAML
3103deprecated: v6.0.0
3104-->
3105
3106> Stability: 0 - Deprecated: Use [`Buffer.allocUnsafeSlow()`][] instead.
3107
3108See [`Buffer.allocUnsafeSlow()`][]. This was never a class in the sense that
3109the constructor always returned a `Buffer` instance, rather than a `SlowBuffer`
3110instance.
3111
3112#### `new SlowBuffer(size)`
3113<!-- YAML
3114deprecated: v6.0.0
3115-->
3116
3117> Stability: 0 - Deprecated: Use [`Buffer.allocUnsafeSlow()`][] instead.
3118
3119* `size` {integer} The desired length of the new `SlowBuffer`.
3120
3121See [`Buffer.allocUnsafeSlow()`][].
3122
3123### Buffer constants
3124<!-- YAML
3125added: v8.2.0
3126-->
3127
3128#### `buffer.constants.MAX_LENGTH`
3129<!-- YAML
3130added: v8.2.0
3131-->
3132
3133* {integer} The largest size allowed for a single `Buffer` instance.
3134
3135On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (~1GB).
3136On 64-bit architectures, this value currently is 2<sup>31</sup> - 1 (~2GB).
3137
3138This value is also available as [`buffer.kMaxLength`][].
3139
3140#### `buffer.constants.MAX_STRING_LENGTH`
3141<!-- YAML
3142added: v8.2.0
3143-->
3144
3145* {integer} The largest length allowed for a single `string` instance.
3146
3147Represents the largest `length` that a `string` primitive can have, counted
3148in UTF-16 code units.
3149
3150This value may depend on the JS engine that is being used.
3151
3152## `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`
3153
3154In versions of Node.js prior to 6.0.0, `Buffer` instances were created using the
3155`Buffer` constructor function, which allocates the returned `Buffer`
3156differently based on what arguments are provided:
3157
3158* Passing a number as the first argument to `Buffer()` (e.g. `new Buffer(10)`)
3159  allocates a new `Buffer` object of the specified size. Prior to Node.js 8.0.0,
3160  the memory allocated for such `Buffer` instances is *not* initialized and
3161  *can contain sensitive data*. Such `Buffer` instances *must* be subsequently
3162  initialized by using either [`buf.fill(0)`][`buf.fill()`] or by writing to the
3163  entire `Buffer` before reading data from the `Buffer`.
3164  While this behavior is *intentional* to improve performance,
3165  development experience has demonstrated that a more explicit distinction is
3166  required between creating a fast-but-uninitialized `Buffer` versus creating a
3167  slower-but-safer `Buffer`. Since Node.js 8.0.0, `Buffer(num)` and `new
3168  Buffer(num)` return a `Buffer` with initialized memory.
3169* Passing a string, array, or `Buffer` as the first argument copies the
3170  passed object's data into the `Buffer`.
3171* Passing an [`ArrayBuffer`][] or a [`SharedArrayBuffer`][] returns a `Buffer`
3172  that shares allocated memory with the given array buffer.
3173
3174Because the behavior of `new Buffer()` is different depending on the type of the
3175first argument, security and reliability issues can be inadvertently introduced
3176into applications when argument validation or `Buffer` initialization is not
3177performed.
3178
3179For example, if an attacker can cause an application to receive a number where
3180a string is expected, the application may call `new Buffer(100)`
3181instead of `new Buffer("100")`, leading it to allocate a 100 byte buffer instead
3182of allocating a 3 byte buffer with content `"100"`. This is commonly possible
3183using JSON API calls. Since JSON distinguishes between numeric and string types,
3184it allows injection of numbers where a naively written application that does not
3185validate its input sufficiently might expect to always receive a string.
3186Before Node.js 8.0.0, the 100 byte buffer might contain
3187arbitrary pre-existing in-memory data, so may be used to expose in-memory
3188secrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot
3189occur because the data is zero-filled. However, other attacks are still
3190possible, such as causing very large buffers to be allocated by the server,
3191leading to performance degradation or crashing on memory exhaustion.
3192
3193To make the creation of `Buffer` instances more reliable and less error-prone,
3194the various forms of the `new Buffer()` constructor have been **deprecated**
3195and replaced by separate `Buffer.from()`, [`Buffer.alloc()`][], and
3196[`Buffer.allocUnsafe()`][] methods.
3197
3198*Developers should migrate all existing uses of the `new Buffer()` constructors
3199to one of these new APIs.*
3200
3201* [`Buffer.from(array)`][] returns a new `Buffer` that *contains a copy* of the
3202  provided octets.
3203* [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`]
3204  returns a new `Buffer` that *shares the same allocated memory* as the given
3205  [`ArrayBuffer`][].
3206* [`Buffer.from(buffer)`][] returns a new `Buffer` that *contains a copy* of the
3207  contents of the given `Buffer`.
3208* [`Buffer.from(string[, encoding])`][`Buffer.from(string)`] returns a new
3209  `Buffer` that *contains a copy* of the provided string.
3210* [`Buffer.alloc(size[, fill[, encoding]])`][`Buffer.alloc()`] returns a new
3211  initialized `Buffer` of the specified size. This method is slower than
3212  [`Buffer.allocUnsafe(size)`][`Buffer.allocUnsafe()`] but guarantees that newly
3213  created `Buffer` instances never contain old data that is potentially
3214  sensitive. A `TypeError` will be thrown if `size` is not a number.
3215* [`Buffer.allocUnsafe(size)`][`Buffer.allocUnsafe()`] and
3216  [`Buffer.allocUnsafeSlow(size)`][`Buffer.allocUnsafeSlow()`] each return a
3217  new uninitialized `Buffer` of the specified `size`. Because the `Buffer` is
3218  uninitialized, the allocated segment of memory might contain old data that is
3219  potentially sensitive.
3220
3221`Buffer` instances returned by [`Buffer.allocUnsafe()`][] and
3222[`Buffer.from(array)`][] *may* be allocated off a shared internal memory pool
3223if `size` is less than or equal to half [`Buffer.poolSize`][]. Instances
3224returned by [`Buffer.allocUnsafeSlow()`][] *never* use the shared internal
3225memory pool.
3226
3227### The `--zero-fill-buffers` command line option
3228<!-- YAML
3229added: v5.10.0
3230-->
3231
3232Node.js can be started using the `--zero-fill-buffers` command line option to
3233cause all newly-allocated `Buffer` instances to be zero-filled upon creation by
3234default. Without the option, buffers created with [`Buffer.allocUnsafe()`][],
3235[`Buffer.allocUnsafeSlow()`][], and `new SlowBuffer(size)` are not zero-filled.
3236Use of this flag can have a measurable negative impact on performance. Use the
3237`--zero-fill-buffers` option only when necessary to enforce that newly allocated
3238`Buffer` instances cannot contain old data that is potentially sensitive.
3239
3240```console
3241$ node --zero-fill-buffers
3242> Buffer.allocUnsafe(5);
3243<Buffer 00 00 00 00 00>
3244```
3245
3246### What makes `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()` "unsafe"?
3247
3248When calling [`Buffer.allocUnsafe()`][] and [`Buffer.allocUnsafeSlow()`][], the
3249segment of allocated memory is *uninitialized* (it is not zeroed-out). While
3250this design makes the allocation of memory quite fast, the allocated segment of
3251memory might contain old data that is potentially sensitive. Using a `Buffer`
3252created by [`Buffer.allocUnsafe()`][] without *completely* overwriting the
3253memory can allow this old data to be leaked when the `Buffer` memory is read.
3254
3255While there are clear performance advantages to using
3256[`Buffer.allocUnsafe()`][], extra care *must* be taken in order to avoid
3257introducing security vulnerabilities into an application.
3258
3259[RFC 4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5
3260[WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/
3261[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
3262[`Buffer.alloc()`]: #buffer_static_method_buffer_alloc_size_fill_encoding
3263[`Buffer.allocUnsafe()`]: #buffer_static_method_buffer_allocunsafe_size
3264[`Buffer.allocUnsafeSlow()`]: #buffer_static_method_buffer_allocunsafeslow_size
3265[`Buffer.concat()`]: #buffer_static_method_buffer_concat_list_totallength
3266[`Buffer.from(array)`]: #buffer_static_method_buffer_from_array
3267[`Buffer.from(arrayBuf)`]: #buffer_static_method_buffer_from_arraybuffer_byteoffset_length
3268[`Buffer.from(buffer)`]: #buffer_static_method_buffer_from_buffer
3269[`Buffer.from(string)`]: #buffer_static_method_buffer_from_string_encoding
3270[`Buffer.poolSize`]: #buffer_class_property_buffer_poolsize
3271[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
3272[`ERR_INVALID_BUFFER_SIZE`]: errors.html#ERR_INVALID_BUFFER_SIZE
3273[`ERR_INVALID_OPT_VALUE`]: errors.html#ERR_INVALID_OPT_VALUE
3274[`ERR_OUT_OF_RANGE`]: errors.html#ERR_OUT_OF_RANGE
3275[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
3276[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
3277[`String#indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
3278[`String#lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
3279[`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
3280[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
3281[`TypedArray#set()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set
3282[`TypedArray#slice()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice
3283[`TypedArray#subarray()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray
3284[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
3285[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
3286[`buf.buffer`]: #buffer_buf_buffer
3287[`buf.compare()`]: #buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend
3288[`buf.entries()`]: #buffer_buf_entries
3289[`buf.fill()`]: #buffer_buf_fill_value_offset_end_encoding
3290[`buf.indexOf()`]: #buffer_buf_indexof_value_byteoffset_encoding
3291[`buf.keys()`]: #buffer_buf_keys
3292[`buf.length`]: #buffer_buf_length
3293[`buf.slice()`]: #buffer_buf_slice_start_end
3294[`buf.toString()`]: #buffer_buf_tostring_encoding_start_end
3295[`buf.values()`]: #buffer_buf_values
3296[`buffer.constants.MAX_LENGTH`]: #buffer_buffer_constants_max_length
3297[`buffer.constants.MAX_STRING_LENGTH`]: #buffer_buffer_constants_max_string_length
3298[`buffer.kMaxLength`]: #buffer_buffer_kmaxlength
3299[`util.inspect()`]: util.html#util_util_inspect_object_options
3300[ASCII]: https://en.wikipedia.org/wiki/ASCII
3301[Base64]: https://en.wikipedia.org/wiki/Base64
3302[ISO-8859-1]: https://en.wikipedia.org/wiki/ISO-8859-1
3303[UTF-8]: https://en.wikipedia.org/wiki/UTF-8
3304[UTF-16]: https://en.wikipedia.org/wiki/UTF-16
3305[binary strings]: https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary
3306[endianness]: https://en.wikipedia.org/wiki/Endianness
3307[iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
3308