Nodejs buffers: Difference between revisions
From wikinotes
(→Basics) |
(→Basics) |
||
Line 19: | Line 19: | ||
* <code>allocUnsafe()</code> only reserves memory (faster). | * <code>allocUnsafe()</code> only reserves memory (faster). | ||
By default, buffers are are stored as a [[ | By default, buffers are are stored as a [[unicode|UTF-8]] array of bytes.<br> | ||
<code>buffer.toString()</code> converts the buffer to a [[ | <code>buffer.toString()</code> converts the buffer to a [[unicode|UTF-8]] string. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> |
Revision as of 02:17, 6 August 2021
Buffers are reserved sections of memory designed to store binary data.
Interact with it like an array of bytes.
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.
alloc()
reserves memory, initializing every byte with a 0allocUnsafe()
only reserves 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);