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.
🌐
Bun
bun.com › binary data › convert an arraybuffer to a buffer
Convert an ArrayBuffer to a Buffer - Bun
July 25, 2026 - Use the static Buffer.from() method to create a Buffer from an ArrayBuffer.
🌐
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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › ArrayBuffer
ArrayBuffer - JavaScript - MDN Web Docs
It is an array of bytes, often ... of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer....
🌐
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); }
🌐
GitHub
miguelmota.com › bytes › arraybuffer-to-buffer
miguelmota (miguel) · GitHub
December 29, 2015 - miguelmota has 550 repositories available. Follow their code on GitHub.
🌐
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....
Find elsewhere
🌐
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....
🌐
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 - It provides a way to handle and manipulate binary data as floating-point numbers. Element Type: 64-bit floating-point (double) numbers. Endianess: Platform-dependent (usually little-endian). Typed Array Features: Provides methods for accessing and modifying binary data efficiently. // 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); // Output: Float64Array(2) [ 3.141592653589793, 2.718281828459045 ]
🌐
techtutorialsx
techtutorialsx.wordpress.com › 2019 › 10 › 27 › node-js-converting-array-to-buffer
Node.js: Converting Array to Buffer – techtutorialsx
October 27, 2019 - Then, to convert the array to a Buffer, we simply need to call the from class method, passing as input our array.
🌐
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.
🌐
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
🌐
Node.js
nodejs.org › download › release › v8.1.2 › docs › api › buffer.html
Buffer | Node.js v8.1.2 Documentation
Starting in Node.js 8.0.0, Buffer(num) ... as the first argument copies the passed object's data into the Buffer. Passing an ArrayBuffer returns a Buffer that shares allocated memory with the given ArrayBuffer....
🌐
Nocin
nocin.eu › nodejs-arraybuffer-to-buffer-uint8array-to-buffer
[nodejs] ArrayBuffer to Buffer | Uint8Array to Buffer – nocin.eu
Skip to content · nocin.eu Homelab, Linux, JS & ABAP (~˘▾˘)~ JavaScript / nodejs · June 8, 2023 June 8, 2023 arrayBuffer, buffer, nodejs, Uint8Array · https://nodejs.org/api/buffer.html · const buffer1 = Buffer.from(ArrayBuffer) const buffer2 = Buffer.from(Uint8Array) This site is best viewed using Dark Reader!
🌐
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.
🌐
GitHub
github.com › nodejs › node › issues › 106
feature: support initializing Buffer from an ArrayBuffer · Issue #106 · nodejs/node
December 8, 2014 - var u = new Uint8Array([1, 2, 3, 4]); var ab = u.buffer; var b = new Buffer(ab); console.log(b); // <Buffer > ... When writing isomorphic code (i.e. code that runs on the server and in the browser), it's often the case that you'll get an ArrayBuffer from a DOM API (xhr, websockets, webrtc, etc.) and need to convert it to a Buffer to work with modules in the npm ecosystem.
Author: nodejs
🌐
Bun
bun.sh › 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.