Buffer and typed arrays, documentation

91 views
Skip to first unread message

Denys Khanzhyiev

unread,
Jun 14, 2014, 5:34:16 PM6/14/14
to nodejs
Hello, I am in search of most efficient way to convert Buffer to ArrayBuffer.
Can somebody explain this paragraph:
"Buffer object can also be used with typed arrays. The buffer object is cloned to an ArrayBuffer that is used as the backing store for the typed array. The memory of the buffer and the ArrayBuffer is not shared."


for me this means that I can do 

var ab = new Uint32Array(new Buffer(16));

and get
ab.length == 4;

but this is not so 
ab.length is 16 

is it a bug? or I get that paragraph wrong?

Trevor Norris

unread,
Jun 15, 2014, 12:03:00 AM6/15/14
to nod...@googlegroups.com
It's copying each octet from the Buffer to the ArrayBuffer. Despite the type of TypedArray. Hence why they have the same length.

In v0.10 Node had its own ArrayBuffer implementation. Now that they are natively supported by V8 that functionality has changed in v0.12.

raul.m...@gmail.com

unread,
Jun 15, 2014, 5:13:39 AM6/15/14
to nod...@googlegroups.com
"Buffer object can also be used with typed arrays. The buffer object is cloned to an ArrayBuffer that is used as the backing store for the typed array.

Not sure if 'cloned' is the most appropriate term here.

var ab = new Uint32Array(new Buffer(16));

and get
ab.length == 4;

but this is not so 
ab.length is 16 

is it a bug? or I get that paragraph wrong?

This is not a bug. ab will have the same length as the parameter. You can, for example, do 

ab = new Uint32Array({ length: 100 });

and ab will have a length of 100.


I think this is the section that you should look for:

TypedArray(type[] array)
Create a new ArrayBuffer with enough bytes to hold array.length elements of this typed array, then creates a typed array view referring to the full buffer. The contents of the new view are initialized to the contents of the given array or typed array, with each element converted to the appropriate typed array type.

Note that the buffer is not an ArrayBuffer so I think this is the way the constructor is called. Also note that the elements from the buffer are converted to the appropriate typed array type. So that's why I think the array buffer is not really cloned, but rather it is 'transformed' into another one. ab.buffer clearly gives you a different buffer than the original array buffer.

Denys Khanzhyiev

unread,
Jun 15, 2014, 5:00:41 PM6/15/14
to nodejs
Though it is possible to get correct UInt32Array 
var a = new Uint32Array((new Uint8Array(new Buffer(16))).buffer);

a.length == 4

I found that it is much faster to use following trick

var
   buffer = new Buffer(N),
   dest = new Uint32Array(buffer.length>>2),
   i = dest.length,o;

while(i--){
 o = i<<2;
 dest[i] = ((b[o]) |
                       (b[o + 1] << 8) |
                       (b[o + 2] << 16)) +
                   (b[o + 3] * 0x1000000);
}

What is strange - that using Uint8Array to copy data from Buffer to ArrayBuffer in a loop is much slower.

Denys Khanzhyiev

unread,
Jun 16, 2014, 1:27:34 AM6/16/14
to nodejs
Yes I understand what is happening.
I just thought there is another special way of "Buffer object can also be used with typed arrays" that I am not aware of.



2014-06-15 7:03 GMT+03:00 Trevor Norris <trev....@gmail.com>:
It's copying each octet from the Buffer to the ArrayBuffer. Despite the type of TypedArray. Hence why they have the same length.

In v0.10 Node had its own ArrayBuffer implementation. Now that they are natively supported by V8 that functionality has changed in v0.12.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/cbad534f-06c2-45b7-8640-a563a2dd4421%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages