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

3
  • 2
    Why byteLength? That seems like an implementation detail, as there's no byteLength property in the API. What's the value of nbuf.length? Commented Jan 26, 2017 at 5:52
  • 1
    @cartant thanks! nbuf.length properly gave 6, but I thought per 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? Commented Jan 26, 2017 at 5: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. Commented Jan 26, 2017 at 6:02

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.