Nodejs buffers: Difference between revisions

From wikinotes
(Created page with "Buffers are reserved sections of memory designed to store binary data. = Documentation = <blockquote> {| class="wikitable" |- | buffer docs || https://nodejs.org/api/buffer.h...")
 
No edit summary
Line 11: Line 11:
|}
|}
</blockquote><!-- Documentation -->
</blockquote><!-- Documentation -->
= Basics =
<blockquote>
Buffers are fixed size, reserved sections of memory.<br>
You can <code>alloc()</code> the entire section of memory (filling it with 0s)<br>
Or you can <code>allocUnsafe</code> which simply reserves the memory (faster).
By default, buffers are are stored as a UTF-8 array of bytes.<br>
<code>buffer.toString()</code> converts the buffer to a UTF-8 string.


<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
const buffer = require('buffer');
import { Buffer } from 'buffer';
 
const buf = Buffer.alloc(10);
const buf = Buffer.allocUnsafe(10);
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Basics -->

Revision as of 23:54, 31 July 2021

Buffers are reserved sections of memory designed to store binary data.

Documentation

buffer docs https://nodejs.org/api/buffer.html
buffer tutorial https://nodejs.dev/learn/nodejs-buffers

Basics

Buffers are fixed size, reserved sections of memory.
You can alloc() the entire section of memory (filling it with 0s)
Or you can allocUnsafe which simply reserves the memory (faster).

By default, buffers are are stored as a UTF-8 array of bytes.
buffer.toString() converts the buffer to a UTF-8 string.

import { Buffer } from 'buffer';

const buf = Buffer.alloc(10);
const buf = Buffer.allocUnsafe(10);