Instances of Buffer are also instances of Uint8Array in node.js 4.x and higher. Thus, the most efficient solution is to access the buffer's own .buffer property directly, as per https://stackoverflow.com/a/31394257/1375574. The Buffer constructor also takes an ArrayBufferView argument if you need to go the other direction.

Note that this will not create a copy, which means that writes to any ArrayBufferView will write through to the original Buffer instance.


In older versions, node.js has both ArrayBuffer as part of v8, but the Buffer class provides a more flexible API. In order to read or write to an ArrayBuffer, you only need to create a view and copy across.

From Buffer to ArrayBuffer:

function toArrayBuffer(buffer) {
  const arrayBuffer = new ArrayBuffer(buffer.length);
  const view = new Uint8Array(arrayBuffer);
  for (let i = 0; i < buffer.length; ++i) {
    view[i] = buffer[i];
  }
  return arrayBuffer;
}

From ArrayBuffer to Buffer:

function toBuffer(arrayBuffer) {
  const buffer = Buffer.alloc(arrayBuffer.byteLength);
  const view = new Uint8Array(arrayBuffer);
  for (let i = 0; i < buffer.length; ++i) {
    buffer[i] = view[i];
  }
  return buffer;
}
Answer from Martin Thomson on Stack Overflow
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.11.1 Documentation
When creating a Buffer using a <TypedArray>'s .buffer, it is possible to use only a portion of the underlying <ArrayBuffer> by passing in byteOffset and length parameters.
🌐
GitHub
gist.github.com › miguelmota › 5b06ae5698877322d0ca
Node.js Buffer to ArrayBuffer · GitHub
function toArrayBuffer(buffer) { return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); } function toBuffer(byteArray) { return Buffer.from(byteArray); }
🌐
npm
npmjs.com › package › buffer-to-arraybuffer
buffer-to-arraybuffer - npm
February 21, 2021 - var bufferToArrayBuffer = require('buffer-to-arraybuffer'); var b = new Buffer(12); b.write('abc', 0); var ab = bufferToArrayBuffer(b); String.fromCharCode.apply(null, new Uint8Array(ab)); // 'abc' NOTE: If you only target node v4.3+, you can simply just do: new Buffer([12]).buffer ·
      » npm install buffer-to-arraybuffer
    
Published: Feb 21, 2021
Version: 0.0.6
🌐
Bun
bun.com › binary data › convert an arraybuffer to a buffer
Convert an ArrayBuffer to a Buffer - Bun
July 25, 2026 - const arrBuffer = new ArrayBuffer(64); const nodeBuffer = Buffer.from(arrBuffer);
🌐
Bun
bun.com › guides › binary data › buffer to arraybuffer
Convert a Buffer to an ArrayBuffer | Bun Guides
The Node.js Buffer class views and manipulates data in an underlying ArrayBuffer.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › ArrayBuffer
ArrayBuffer - JavaScript - MDN Web Docs
Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's bytes from begin (inclusive) up to end (exclusive). If either begin or end is negative, it refers to an index from the end of the array, as opposed to from the beginning. ... Creates a new ArrayBuffer with the same byte ...
🌐
Medium
dev-aditya.medium.com › buffer-arraybuffer-float64array-in-node-js-ef68c112d905
Buffer, ArrayBuffer, Float64Array in Node.js | by Aditya Yadav | Medium
September 29, 2024 - // Create an ArrayBuffer of 16 bytes const buffer = new ArrayBuffer(16); // Create a Float64Array view const float64View = new Float64Array(buffer); // Assign values float64View[0] = Math.PI; float64View[1] = Math.E; console.log(float64View); ...
🌐
Bun
bun.sh › guides › binary data › arraybuffer to buffer
Convert an ArrayBuffer to a Buffer | Bun Guides
To create a Buffer that only views a portion of the underlying buffer, pass the offset and length to Buffer.from(). const arrBuffer = new ArrayBuffer(64); const nodeBuffer = Buffer.from(arrBuffer, 0, 16); // view first 16 bytes
Find elsewhere
🌐
npm
npmjs.com › package › arraybuffer-to-buffer
arraybuffer-to-buffer - npm
August 1, 2019 - var arrayBufferToBuffer = require('arraybuffer-to-buffer'); var ab = new ArrayBuffer(12); var v = new DataView(ab); [].slice.call('abc').forEach(function(s, i) { v[i] = s.charCodeAt(0); }); var b = arrayBufferToBuffer(ab); b.toString('utf8', 0, 3); // 'abc' MIT ·
      » npm install arraybuffer-to-buffer
    
Published: Aug 01, 2019
Version: 0.0.7
Author: Miguel Mota
🌐
npm
npmjs.com › package › to-arraybuffer
to-arraybuffer - npm
January 13, 2016 - This module is designed to work both in node.js and in all browsers with ArrayBuffer support when using the Buffer implementation provided by Browserify.
      » npm install to-arraybuffer
    
Published: Jan 13, 2016
Version: 1.0.1
🌐
TutorialsPoint
tutorialspoint.com › article › How-to-convert-a-binary-NodeJS-Buffer-to-JavaScript-ArrayBuffer
How to convert a binary NodeJS Buffer to JavaScript ArrayBuffer?
June 24, 2020 - Use buf.buffer for quick access when size doesn't matter, or buf.slice().buffer when you need an exact-sized ArrayBuffer. Manual copying offers the most control but is slower for large buffers.
🌐
Node.js
nodejs.org › download › release › v10.2.0 › docs › api › buffer.html
Buffer | Node.js v10.2.0 Documentation
Starting in Node.js 8.0.0, Buffer(num) ... argument copies the passed object's data into the Buffer. Passing an ArrayBuffer or a SharedArrayBuffer returns a Buffer that shares allocated memory with the given array buffer....
🌐
GitHub
miguelmota.com › bytes › arraybuffer-to-buffer
miguelmota (miguel) · GitHub
December 29, 2015 - miguelmota has 550 repositories available. Follow their code on GitHub.
🌐
xjavascript
xjavascript.com › blog › convert-a-binary-nodejs-buffer-to-javascript-arraybuffer
How to Convert a NodeJS Binary Buffer to a JavaScript ArrayBuffer — xjavascript.com
Only for text data: This method corrupts non-text binary data (e.g., images, audio) because converting raw binary to a string interprets bytes as text characters, which may not map correctly. Inefficient: Involves intermediate string conversion, making it slower than direct ArrayBuffer access. Assuming buffer.buffer is a copy: It’s a reference to the underlying ArrayBuffer.
🌐
Node.js
nodejs.org › download › release › v9.10.1 › docs › api › buffer.html
Buffer | Node.js v9.10.1 Documentation
Starting in Node.js 8.0.0, Buffer(num) ... argument copies the passed object's data into the Buffer. Passing an ArrayBuffer or a SharedArrayBuffer returns a Buffer that shares allocated memory with the given array buffer....
🌐
Pavel Romanov
pavel-romanov.com › understanding-nodejs-buffer
Node.js Buffer Overview - Pavel Romanov
August 16, 2024 - Buffer is Node.js abstraction that enables you to create and manage buffers through a common interface. The Buffer extends Uint8Array and essentially not a buffer, but view into the underlying ArrayBuffer.
🌐
W3Schools
w3schools.com › nodejs › nodejs_buffer.asp
Node.js Buffer Module
Only use it when you understand the security implications and plan to immediately fill the entire buffer. Creates a new Buffer from various sources like strings, arrays, or ArrayBuffer.
🌐
Educative
educative.io › answers › what-is-bufferfrom-in-nodejs
What is Buffer.from() in Node.js?
Note: The underlying array and the buffer share the same memory. The following example demonstrates how to use ArrayBuffer to create a buffer though the Buffer.from(arrayBuffer[, byteOffset[, length]]) function.