<li><ahref="#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a>
<li><ahref="#buffer_the_zero_fill_buffers_command_line_option">The <code>--zero-fill-buffers</code> command line option</a></li>
<li><ahref="#buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li>
<p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many
Node.js APIs support <code>Buffer</code>s.</p>
<p>The <code>Buffer</code> class is a subclass of JavaScript's <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> class and
extends it with methods that cover additional use cases. Node.js APIs accept
plain <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>s wherever <code>Buffer</code>s are supported as well.</p>
<h2>Buffers and character encodings<span><aclass="mark"href="#buffer_buffers_and_character_encodings"id="buffer_buffers_and_character_encodings">#</a></span></h2>
<p>The character encodings currently supported by Node.js are the following:</p>
<ul>
<li>
<p><code>'utf8'</code>: Multi-byte encoded Unicode characters. Many web pages and other
document formats use <ahref="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default character encoding.
When decoding a <code>Buffer</code> into a string that does not exclusively contain
valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code><20> will be used
to represent those errors.</p>
</li>
<li>
<p><code>'utf16le'</code>: Multi-byte encoded Unicode characters. Unlike <code>'utf8'</code>, each
character in the string will be encoded using either 2 or 4 bytes.
Node.js only supports the <ahref="https://en.wikipedia.org/wiki/Endianness">little-endian</a> variant of <ahref="https://en.wikipedia.org/wiki/UTF-16">UTF-16</a>.</p>
</li>
<li>
<p><code>'latin1'</code>: Latin-1 stands for <ahref="https://en.wikipedia.org/wiki/ISO-8859-1">ISO-8859-1</a>. This character encoding only
supports the Unicode characters from <code>U+0000</code> to <code>U+00FF</code>. Each character is
encoded using a single byte. Characters that do not fit into that range are
truncated and will be mapped to characters in that range.</p>
</li>
</ul>
<p>Converting a <code>Buffer</code> into a string using one of the above is referred to as
decoding, and converting a string into a <code>Buffer</code> is referred to as encoding.</p>
<p>Node.js also supports the following two binary-to-text encodings. For
binary-to-text encodings, the naming convention is reversed: Converting a
<code>Buffer</code> into a string is typically referred to as encoding, and converting a
string into a <code>Buffer</code> as decoding.</p>
<ul>
<li>
<p><code>'base64'</code>: <ahref="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding. When creating a <code>Buffer</code> from a string,
this encoding will also correctly accept "URL and Filename Safe Alphabet" as
each byte before decoding as <code>'latin1'</code>.
Generally, there should be no reason to use this encoding, as <code>'utf8'</code>
(or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a
better choice when encoding or decoding ASCII-only text. It is only provided
for legacy compatibility.</p>
</li>
<li>
<p><code>'binary'</code>: Alias for <code>'latin1'</code>. See <ahref="https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary">binary strings</a> for more background
on this topic. The name of this encoding can be very misleading, as all of the
encodings listed here convert between strings and binary data. For converting
between strings and <code>Buffer</code>s, typically <code>'utf-8'</code> is the right choice.</p>
</li>
<li>
<p><code>'ucs2'</code>: Alias of <code>'utf16le'</code>. UCS-2 used to refer to a variant of UTF-16
that did not support characters that had code points larger than U+FFFF.
In Node.js, these code points are always supported.</p>
<p><code>Buffer</code> instances are also JavaScript <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> and <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>
instances. All <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> methods are available on <code>Buffer</code>s. There are,
however, subtle incompatibilities between the <code>Buffer</code> API and the
<li>While <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice"><code>TypedArray#slice()</code></a> creates a copy of part of the <code>TypedArray</code>,
<ahref="#buffer_buf_slice_start_end"><code>Buffer#slice()</code></a> creates a view over the existing <code>Buffer</code>
without copying. This behavior can be surprising, and only exists for legacy
compatibility. <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray"><code>TypedArray#subarray()</code></a> can be used to achieve the behavior
of <ahref="#buffer_buf_slice_start_end"><code>Buffer#slice()</code></a> on both <code>Buffer</code>s and other
<code>TypedArray</code>s.</li>
<li><ahref="#buffer_buf_tostring_encoding_start_end"><code>buf.toString()</code></a> is incompatible with its <code>TypedArray</code> equivalent.</li>
<li>A number of methods, e.g. <ahref="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf()</code></a>, support additional arguments.</li>
<p>There are two ways to create new <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instances from a <code>Buffer</code>:</p>
<ul>
<li>Passing a <code>Buffer</code> to a <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> constructor will copy the <code>Buffer</code>s
contents, interpreted as an array of integers, and not as a byte sequence
<li>Passing the <code>Buffer</code>s underlying <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> will create a
<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> that shares its memory with the <code>Buffer</code>.</li>
memory as a <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instance by using the <code>TypedArray</code> object’s
<p>When creating a <code>Buffer</code> using a <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>'s <code>.buffer</code>, it is
possible to use only a portion of the underlying <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> by passing in
<code>byteOffset</code> and <code>length</code> parameters.</p>
<p>The <code>Buffer.from()</code> and <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from"><code>TypedArray.from()</code></a> have different signatures and
implementations. Specifically, the <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> variants accept a second
argument that is a mapping function that is invoked on every element of the
<li><code>size</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The desired length of the new <code>Buffer</code>.</li>
<li><code>fill</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> A value to pre-fill the new <code>Buffer</code>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>fill</code> is a string, this is its encoding.
<ahref="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <ahref="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a>
is thrown.</p>
<p>If <code>fill</code> is specified, the allocated <code>Buffer</code> will be initialized by calling
<p>Calling <ahref="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> can be measurably slower than the alternative
<ahref="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> but ensures that the newly created <code>Buffer</code> instance
<li><code>size</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The desired length of the new <code>Buffer</code>.</li>
</ul>
<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than
<ahref="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <ahref="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a>
is thrown.</p>
<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not
initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and
<em>may contain sensitive data</em>. Use <ahref="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> instead to initialize
<code>Buffer</code> instances created using <ahref="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>,
<ahref="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a>, <ahref="#buffer_static_method_buffer_concat_list_totallength"><code>Buffer.concat()</code></a>, and the deprecated
<code>new Buffer(size)</code> constructor only when <code>size</code> is less than or equal
to <code>Buffer.poolSize >> 1</code> (floor of <ahref="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a> divided by two).</p>
<p>Use of this pre-allocated internal memory pool is a key difference between
calling <code>Buffer.alloc(size, fill)</code> vs. <code>Buffer.allocUnsafe(size).fill(fill)</code>.
Specifically, <code>Buffer.alloc(size, fill)</code> will <em>never</em> use the internal <code>Buffer</code>
pool, while <code>Buffer.allocUnsafe(size).fill(fill)</code><em>will</em> use the internal
<code>Buffer</code> pool if <code>size</code> is less than or equal to half <ahref="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a>. The
difference is subtle but can be important when an application requires the
<li><code>size</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The desired length of the new <code>Buffer</code>.</li>
</ul>
<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than
<ahref="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <ahref="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a>
is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>
<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not
initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and
<em>may contain sensitive data</em>. Use <ahref="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(0)</code></a> to initialize
such <code>Buffer</code> instances with zeroes.</p>
<p>When using <ahref="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> to allocate new <code>Buffer</code> instances,
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>string</code> is a string, this is its encoding.
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The number of bytes contained within <code>string</code>.</li>
</ul>
<p>Returns the byte length of a string when encoded using <code>encoding</code>.
This is not the same as <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length"><code>String.prototype.length</code></a>, which does not account
for the encoding that is used to convert the string into bytes.</p>
<p>For <code>'base64'</code> and <code>'hex'</code>, this function assumes valid input. For strings that
contain non-base64/hex-encoded data (e.g. whitespace), the return value might be
greater than the length of a <code>Buffer</code> created from the string.</p>
<p>When <code>string</code> is a <code>Buffer</code>/<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a>/<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>/<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>/
<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a>, the byte length as reported by <code>.byteLength</code>
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Either <code>-1</code>, <code>0</code>, or <code>1</code>, depending on the result of the
comparison. See <ahref="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare()</code></a> for details.</li>
</ul>
<p>Compares <code>buf1</code> to <code>buf2</code>, typically for the purpose of sorting arrays of
<code>Buffer</code> instances. This is equivalent to calling
<li><code>list</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer[]></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array[]></a> List of <code>Buffer</code> or <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>
instances to concatenate.</li>
<li><code>totalLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Total length of the <code>Buffer</code> instances in <code>list</code>
<p><code>Buffer.from(array)</code> and <ahref="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string)</code></a> may also use the internal
<code>Buffer</code> pool like <ahref="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> does.</p>
<li><code>arrayBuffer</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"class="type"><ArrayBuffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"class="type"><SharedArrayBuffer></a> An <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>,
<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a>, for example the <code>.buffer</code> property of a
<li><code>byteOffset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Index of first byte to expose. <strong>Default:</strong><code>0</code>.</li>
<li><code>length</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to expose.
<p>This creates a view of the <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> without copying the underlying
memory. For example, when passed a reference to the <code>.buffer</code> property of a
<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instance, the newly created <code>Buffer</code> will share the same
allocated memory as the <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</p>
<pre><codeclass="language-js"><spanclass="hljs-keyword">const</span> ab = <spanclass="hljs-keyword">new</span><spanclass="hljs-built_in">ArrayBuffer</span>(<spanclass="hljs-number">10</span>);
<p>A <code>TypeError</code> will be thrown if <code>arrayBuffer</code> is not an <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> or a
<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> or another type appropriate for <code>Buffer.from()</code>
<li><code>buffer</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> An existing <code>Buffer</code> or <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> from
which to copy data.</li>
</ul>
<p>Copies the passed <code>buffer</code> data onto a new <code>Buffer</code> instance.</p>
<li><code>object</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object"class="type"><Object></a> An object supporting <code>Symbol.toPrimitive</code> or <code>valueOf()</code>.</li>
<li><code>offsetOrEncoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> A byte-offset or encoding.</li>
<li><code>length</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> A length.</li>
<pre><codeclass="language-js"><spanclass="hljs-keyword">const</span> buf = Buffer.from(<spanclass="hljs-keyword">new</span><spanclass="hljs-built_in">String</span>(<spanclass="hljs-string">'this is a test'</span>));
<li><code>string</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> A string to encode.</li>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The encoding of <code>string</code>. <strong>Default:</strong><code>'utf8'</code>.</li>
</ul>
<p>Creates a new <code>Buffer</code> containing <code>string</code>. The <code>encoding</code> parameter identifies
the character encoding to be used when converting <code>string</code> into bytes.</p>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> A character encoding name to check.</li>
<li><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"class="type"><ArrayBuffer></a> The underlying <code>ArrayBuffer</code> object based on which this <code>Buffer</code>
<li><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The <code>byteOffset</code> of the <code>Buffer</code>s underlying <code>ArrayBuffer</code> object.</li>
<li><code>target</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> A <code>Buffer</code> or <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> with which to
compare <code>buf</code>.</li>
<li><code>targetStart</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The offset within <code>target</code> at which to begin
<li><code>targetEnd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The offset within <code>target</code> at which to end comparison
<li><code>sourceStart</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The offset within <code>buf</code> at which to begin comparison.
<strong>Default:</strong><code>0</code>.</li>
<li><code>sourceEnd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The offset within <code>buf</code> at which to end comparison
<p><ahref="errors.html#ERR_OUT_OF_RANGE"><code>ERR_OUT_OF_RANGE</code></a> is thrown if <code>targetStart < 0</code>, <code>sourceStart < 0</code>,
<code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p>
<li><code>target</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> A <code>Buffer</code> or <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> to copy into.</li>
<li><code>targetStart</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The offset within <code>target</code> at which to begin
<li><code>sourceStart</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The offset within <code>buf</code> from which to begin copying.
<strong>Default:</strong><code>0</code>.</li>
<li><code>sourceEnd</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The offset within <code>buf</code> at which to stop copying (not
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The number of bytes copied.</li>
</ul>
<p>Copies data from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</code>
memory region overlaps with <code>buf</code>.</p>
<p><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set"><code>TypedArray#set()</code></a> performs the same operation, and is available for all
TypedArrays, including Node.js <code>Buffer</code>s, although it takes different
<spanclass="hljs-keyword">for</span> (<spanclass="hljs-keyword">let</span> i = <spanclass="hljs-number">0</span>; i <<spanclass="hljs-number">26</span>; i++) {
<spanclass="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
<spanclass="hljs-keyword">for</span> (<spanclass="hljs-keyword">let</span> i = <spanclass="hljs-number">0</span>; i <<spanclass="hljs-number">26</span>; i++) {
<spanclass="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
<p>Creates and returns an <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> of <code>[index, byte]</code> pairs from the contents
<li><code>otherBuffer</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> A <code>Buffer</code> or <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> with which to
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The value with which to fill <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to fill <code>buf</code>.
<strong>Default:</strong><code>0</code>.</li>
<li><code>end</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Where to stop filling <code>buf</code> (not inclusive). <strong>Default:</strong>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The encoding for <code>value</code> if <code>value</code> is a string.
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> What to search for.</li>
<li><code>byteOffset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Where to begin searching in <code>buf</code>. If negative, then
offset is calculated from the end of <code>buf</code>. <strong>Default:</strong><code>0</code>.</li>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>value</code> is a string, this is its encoding.
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type"class="type"><boolean></a><code>true</code> if <code>value</code> was found in <code>buf</code>, <code>false</code> otherwise.</li>
</ul>
<p>Equivalent to <ahref="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf() !== -1</code></a>.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> What to search for.</li>
<li><code>byteOffset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Where to begin searching in <code>buf</code>. If negative, then
offset is calculated from the end of <code>buf</code>. <strong>Default:</strong><code>0</code>.</li>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>value</code> is a string, this is the encoding used to
determine the binary representation of the string that will be searched for in
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The index of the first occurrence of <code>value</code> in <code>buf</code>, or
<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
</ul>
<p>If <code>value</code> is:</p>
<ul>
<li>a string, <code>value</code> is interpreted according to the character encoding in
<code>encoding</code>.</li>
<li>a <code>Buffer</code> or <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>, <code>value</code> will be used in its entirety.
To compare a partial <code>Buffer</code>, use <ahref="#buffer_buf_slice_start_end"><code>buf.slice()</code></a>.</li>
<li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer
value between <code>0</code> and <code>255</code>.</li>
<p>Creates and returns an <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> of <code>buf</code> keys (indices).</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> | <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> What to search for.</li>
<li><code>byteOffset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Where to begin searching in <code>buf</code>. If negative, then
offset is calculated from the end of <code>buf</code>. <strong>Default:</strong>
<code>buf.length - 1</code>.</li>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> If <code>value</code> is a string, this is the encoding used to
determine the binary representation of the string that will be searched for in
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The index of the last occurrence of <code>value</code> in <code>buf</code>, or
<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
</ul>
<p>Identical to <ahref="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf()</code></a>, except the last occurrence of <code>value</code> is found
<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a
<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value,
an integer between 0 and 255.</p>
<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. Any arguments
that coerce to <code>NaN</code>, like <code>{}</code> or <code>undefined</code>, will search the whole buffer.
This behavior matches <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf"><code>String#lastIndexOf()</code></a>.</p>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>byteLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to read. Must satisfy
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>byteLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to read. Must satisfy
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>byteLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to read. Must satisfy
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to read. Must
<li><code>byteLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to read. Must satisfy
<li><code>start</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Where the new <code>Buffer</code> will start. <strong>Default:</strong><code>0</code>.</li>
<li><code>end</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Where the new <code>Buffer</code> will end (not inclusive).
<p>Returns a new <code>Buffer</code> that references the same memory as the original, but
offset and cropped by the <code>start</code> and <code>end</code> indices.</p>
<p>Specifying <code>end</code> greater than <ahref="#buffer_buf_length"><code>buf.length</code></a> will return the same result as
that of <code>end</code> equal to <ahref="#buffer_buf_length"><code>buf.length</code></a>.</p>
<p>This method is inherited from <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray"><code>TypedArray#subarray()</code></a>.</p>
<p>Modifying the new <code>Buffer</code> slice will modify the memory in the original <code>Buffer</code>
because the allocated memory of the two objects overlap.</p>
<spanclass="hljs-keyword">for</span> (<spanclass="hljs-keyword">let</span> i = <spanclass="hljs-number">0</span>; i <<spanclass="hljs-number">26</span>; i++) {
<spanclass="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
<li><code>start</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Where the new <code>Buffer</code> will start. <strong>Default:</strong><code>0</code>.</li>
<li><code>end</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Where the new <code>Buffer</code> will end (not inclusive).
<li>Returns: <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> A reference to <code>buf</code>.</li>
</ul>
<p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the
byte order <em>in-place</em>. Throws <ahref="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <ahref="#buffer_buf_length"><code>buf.length</code></a>
<li>Returns: <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> A reference to <code>buf</code>.</li>
</ul>
<p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the
byte order <em>in-place</em>. Throws <ahref="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <ahref="#buffer_buf_length"><code>buf.length</code></a>
<li>Returns: <ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> A reference to <code>buf</code>.</li>
</ul>
<p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>.
Throws <ahref="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <ahref="#buffer_buf_length"><code>buf.length</code></a> is not a multiple of 8.</p>
<p>Returns a JSON representation of <code>buf</code>. <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a> implicitly calls
this function when stringifying a <code>Buffer</code> instance.</p>
<p><code>Buffer.from()</code> accepts objects in the format returned from this method.
In particular, <code>Buffer.from(buf.toJSON())</code> works like <code>Buffer.from(buf)</code>.</p>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The character encoding to use. <strong>Default:</strong><code>'utf8'</code>.</li>
<li><code>start</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The byte offset to start decoding at. <strong>Default:</strong><code>0</code>.</li>
<li><code>end</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The byte offset to stop decoding at (not inclusive).
<spanclass="hljs-keyword">for</span> (<spanclass="hljs-keyword">let</span> i = <spanclass="hljs-number">0</span>; i <<spanclass="hljs-number">26</span>; i++) {
<spanclass="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
<p>Creates and returns an <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> for <code>buf</code> values (bytes). This function is
called automatically when a <code>Buffer</code> is used in a <code>for..of</code> statement.</p>
<li><code>string</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> String to write to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write <code>string</code>.
<strong>Default:</strong><code>0</code>.</li>
<li><code>length</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Maximum number of bytes to write (written bytes will not
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The character encoding of <code>string</code>. <strong>Default:</strong><code>'utf8'</code>.</li>
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes written.</li>
</ul>
<p>Writes <code>string</code> to <code>buf</code> at <code>offset</code> according to the character encoding in
<code>encoding</code>. The <code>length</code> parameter is the number of bytes to write. If <code>buf</code> did
not contain enough space to fit the entire string, only part of <code>string</code> will be
written. However, partially encoded characters will not be written.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"class="type"><bigint></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"class="type"><bigint></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.</p>
<p><code>value</code> is interpreted and written as a two's complement signed integer.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"class="type"><bigint></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"class="type"><bigint></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
must be a JavaScript number. Behavior is undefined when <code>value</code> is anything
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><number></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Behavior is
undefined when <code>value</code> is anything other than a JavaScript number.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a valid
signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than
a signed 8-bit integer.</p>
<p><code>value</code> is interpreted and written as a two's complement signed integer.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
anything other than a signed 16-bit integer.</p>
<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
anything other than a signed 32-bit integer.</p>
<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li><code>byteLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to write. Must satisfy
<code>0 < byteLength <= 6</code>.</li>
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li><code>byteLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to write. Must satisfy
<code>0 < byteLength <= 6</code>.</li>
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
when <code>value</code> is anything other than a signed integer.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a
valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is
anything other than an unsigned 16-bit integer.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is
anything other than an unsigned 32-bit integer.</p>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li><code>byteLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to write. Must satisfy
<code>0 < byteLength <= 6</code>.</li>
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
<li><code>value</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number to be written to <code>buf</code>.</li>
<li><code>offset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to skip before starting to write. Must
<li><code>byteLength</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to write. Must satisfy
<code>0 < byteLength <= 6</code>.</li>
<li>Returns: <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a><code>offset</code> plus the number of bytes written.</li>
</ul>
<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
when <code>value</code> is anything other than an unsigned integer.</p>
<li><code>array</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer[]></a> An array of bytes to copy from.</li>
<li><code>arrayBuffer</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"class="type"><ArrayBuffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"class="type"><SharedArrayBuffer></a> An <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>,
<ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> or the <code>.buffer</code> property of a <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li>
<li><code>byteOffset</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Index of first byte to expose. <strong>Default:</strong><code>0</code>.</li>
<li><code>length</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> Number of bytes to expose.
<li><code>buffer</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> An existing <code>Buffer</code> or <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> from
<p></p><divclass="api_stability api_stability_0"><ahref="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <ahref="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> instead (also see
<li><code>size</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The desired length of the new <code>Buffer</code>.</li>
<p>See <ahref="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> and <ahref="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>. This variant of the
constructor is equivalent to <ahref="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a>.</p>
<li><code>string</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> String to encode.</li>
<li><code>encoding</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The encoding of <code>string</code>. <strong>Default:</strong><code>'utf8'</code>.</li>
<li><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The largest size allowed for a single <code>Buffer</code> instance.</li>
</ul>
<p>An alias for <ahref="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p>
<li><code>source</code><ahref="buffer.html#buffer_class_buffer"class="type"><Buffer></a> | <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"class="type"><Uint8Array></a> A <code>Buffer</code> or <code>Uint8Array</code> instance.</li>
<li><code>fromEnc</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> The current encoding.</li>
<li><code>toEnc</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type"class="type"><string></a> To target encoding.</li>
<li><code>size</code><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The desired length of the new <code>SlowBuffer</code>.</li>
<li><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The largest size allowed for a single <code>Buffer</code> instance.</li>
<li><ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type"class="type"><integer></a> The largest length allowed for a single <code>string</code> instance.</li>
</ul>
<p>Represents the largest <code>length</code> that a <code>string</code> primitive can have, counted
in UTF-16 code units.</p>
<p>This value may depend on the JS engine that is being used.</p>
<h2><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code><span><aclass="mark"href="#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe"id="buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe">#</a></span></h2>
<p>In versions of Node.js prior to 6.0.0, <code>Buffer</code> instances were created using the
<code>Buffer</code> constructor function, which allocates the returned <code>Buffer</code>
differently based on what arguments are provided:</p>
<ul>
<li>Passing a number as the first argument to <code>Buffer()</code> (e.g. <code>new Buffer(10)</code>)
allocates a new <code>Buffer</code> object of the specified size. Prior to Node.js 8.0.0,
the memory allocated for such <code>Buffer</code> instances is <em>not</em> initialized and
<em>can contain sensitive data</em>. Such <code>Buffer</code> instances <em>must</em> be subsequently
initialized by using either <ahref="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(0)</code></a> or by writing to the
entire <code>Buffer</code> before reading data from the <code>Buffer</code>.
While this behavior is <em>intentional</em> to improve performance,
development experience has demonstrated that a more explicit distinction is
required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a
slower-but-safer <code>Buffer</code>. Since Node.js 8.0.0, <code>Buffer(num)</code> and <code>new Buffer(num)</code> return a <code>Buffer</code> with initialized memory.</li>
<li>Passing a string, array, or <code>Buffer</code> as the first argument copies the
passed object's data into the <code>Buffer</code>.</li>
<li>Passing an <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> or a <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> returns a <code>Buffer</code>
that shares allocated memory with the given array buffer.</li>
</ul>
<p>Because the behavior of <code>new Buffer()</code> is different depending on the type of the
first argument, security and reliability issues can be inadvertently introduced
into applications when argument validation or <code>Buffer</code> initialization is not
performed.</p>
<p>For example, if an attacker can cause an application to receive a number where
a string is expected, the application may call <code>new Buffer(100)</code>
instead of <code>new Buffer("100")</code>, leading it to allocate a 100 byte buffer instead
of allocating a 3 byte buffer with content <code>"100"</code>. This is commonly possible
using JSON API calls. Since JSON distinguishes between numeric and string types,
it allows injection of numbers where a naively written application that does not
validate its input sufficiently might expect to always receive a string.
Before Node.js 8.0.0, the 100 byte buffer might contain
arbitrary pre-existing in-memory data, so may be used to expose in-memory
and replaced by separate <code>Buffer.from()</code>, <ahref="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a>, and
<li><ahref="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the
<li><ahref="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the
<h3>The <code>--zero-fill-buffers</code> command line option<span><aclass="mark"href="#buffer_the_zero_fill_buffers_command_line_option"id="buffer_the_zero_fill_buffers_command_line_option">#</a></span></h3>
<divclass="api_metadata">
<span>Added in: v5.10.0</span>
</div>
<p>Node.js can be started using the <code>--zero-fill-buffers</code> command line option to
cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by
default. Without the option, buffers created with <ahref="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>,
<ahref="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled.
<h3>What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?<span><aclass="mark"href="#buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe"id="buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe">#</a></span></h3>
<p>When calling <ahref="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> and <ahref="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>, the