node.js - Creating a typed array from a byte array stored as a node Buffer -
from node docs regarding creation of typed arrays buffers:
the buffer's memory interpreted array, not byte array. is,
new uint32array(new buffer([1,2,3,4]))creates 4-elementuint32arrayelements[1,2,3,4], notuint32arraysingle element[0x1020304]or[0x4030201].
this contrasts plain javascript, creating typed array view arraybuffer uses arraybuffer's memory bytes. need behavior in node when operating on node buffers.
i convert buffer arraybuffer, slow application. (i've tried many methods -- they're o(n) time.) (edit: fastest method i've found this, single memmove op , pretty fast, still has @ least momentary 2x memory consumption until reference original buffer released.)
is there (fast/o(1)) way typed array buffer, using buffer's contents bytes instead of elements? (the needed typed array element size >1 byte, needless say.)
as far know, not possible without making copy of data in memory. example new uint32array(new buffer([1,2,3,4])) internally (meaning it's not o(1)).
note typed arrays views of arraybuffer (not buffer, that's not possible). new uint32array(array) creates arraybuffer of 4 * array.length bytes. can access uint32array.buffer. constructor treats buffer no different normal array.
the best solution know the 1 found.
another issue using uint32array try depends on platform byte order. either iterate on buffer this or use dataview if want safe.
Comments
Post a Comment