1

To create a utf-8 buffer from a string in javascript on the web you do this:

var message = JSON.stringify('ping');
var buf = new TextEncoder().encode(message).buffer;
console.log('buf:', buf);
console.log('buf.buffer.byteLength:', buf.byteLength);

This logs:

buf: ArrayBuffer { byteLength: 6 } 
buf.buffer.byteLength: 6

However in Node.js if I do this:

var nbuf = Buffer.from(message, 'utf8');
console.log('nbuf:', nbuf);
console.log('nbuf.buffer:', nbuf.buffer);
console.log('nbuf.buffer.byteLength:', nbuf.buffer.byteLength);

it logs this:

nbuf: <Buffer 22 70 69 6e 67 22>
nbuf.buffer: ArrayBuffer { byteLength: 8192 }
nbuf.buffer.byteLength: 8192

The byteLength is way to high. Am I doing something wrong here?

Thanks

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 2
    Why `byteLength`? That seems like an implementation detail, as there's no `byteLength` property in the [API](https://nodejs.org/api/buffer.html). What's the value of `nbuf.length`? – cartant Jan 26 '17 at 05:52
  • 1
    @cartant thanks! `nbuf.length` properly gave `6`, but I thought per http://stackoverflow.com/a/12101012/1828637 that `Buffer` is just a `Uint8Array` and doing `nbuf.buffer` gives me a javascript `ArrayBuffer` which has `byteLength`. Howcome the `byteLengthf` of the ArrayBuffer in nbuf is 8192? – Noitidart Jan 26 '17 at 05:57
  • 2
    I seem to remember reading that `Buffer` instances are allocated out of larger, underlying buffers - perhaps that's to what the inner `buffer` property refers? - but I'm no expert on the inner workings of Node, so I don't know the definitive answer to that. – cartant Jan 26 '17 at 06:02

1 Answers1

2

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer

ArrayBuffer.prototype.byteLength Read only
The size, in bytes, of the array. This is established when the array is constructed and cannot be changed. Read only.

It seems you should not assume byteLength property to be equal to the actual byte length occupied by the elements in the ArrayBuffer.

In order to get the actual byte length, I suggest using Buffer.byteLength(string[, encoding])

Documentation: https://nodejs.org/api/buffer.html#buffer_class_method_buffer_bytelength_string_encoding

For example,

var message = JSON.stringify('ping');
console.log('byteLength: ', Buffer.byteLength(message));

correctly gives

byteLength: 6

Jeonghyeon Lee
  • 244
  • 1
  • 7