English 中文(简体)
正在添加对应的队列缓冲
原标题:Appending ArrayBuffers

附加/组合阵列布弗斯的最佳方式是什么?

我接收和解析包含各种数据结构的网络包。 接收的信件会读入 ArrayBuffers 。 如果收到部分的包, 我需要保存它, 等待下一个信件, 然后再尝试解析它 。

目前我正在做这样的事:

function appendBuffer( buffer1, buffer2 ) {
  var tmp = new Uint8Array( buffer1.byteLength + buffer2.byteLength );
  tmp.set( new Uint8Array( buffer1 ), 0 );
  tmp.set( new Uint8Array( buffer2 ), buffer1.byteLength );
  return tmp.buffer;
}

显然,您无法绕过时间来创建一个新的缓冲, 因为ArrayBuffers 是一个固定长度, 但是否有必要初始化打印阵列? 到达后我只想能够将缓冲作为缓冲处理; 类型和结构无关紧要 。

问题回答

为何不使用Blob? (我知道当时可能没有)

只要用您的数据创建一个布洛布, 比如 var blob = new blob ([array1,array2,string,...]] = new blob ([array1,array2,string,...]]), 并使用 FileReader (见 < a href=> "https://stackoverflow.com/ questions/153341912/javastant- how-to-go- from- blob- to- arraybuffer" > 将它转换成一个Arraybuffer (如果需要的话) (见 < a href=") 。

Check this : What s the difference between BlobBuilder and the new Blob constructor? And this : MDN Blob API

<强 > EDIT:

我想比较这两种方法(Blobs和问题所使用的方法)的效率,并创建了JSPerf:http://jsperf.com/ append-arraybuffers>http://jsperf.com/ append-arraybuffers>http://jsperf.com/ append-arraybuffers

Seems like using Blobs is slower (In fact, I guess it s the use of Filereader to read the Blob that takes the most time). So now you know ;) Maybe it would me more efficient when there are more than 2 ArrayBuffer (like reconstructing a file from its chunks).

function concat (views: ArrayBufferView[]) {
    let length = 0
    for (const v of views)
        length += v.byteLength
        
    let buf = new Uint8Array(length)
    let offset = 0
    for (const v of views) {
        const uint8view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength)
        buf.set(uint8view, offset)
        offset += uint8view.byteLength
    }
    
    return buf
}

您似乎已经得出结论, 无法创建新的数组缓冲 。 但是, 为了性能, 将缓冲的内容附加到标准数组对象中, 然后创建新的数组缓冲 或从中打字的数组, 可能是有益的 。

var data = [];

function receive_buffer(buffer) {
    var i, len = data.length;

    for(i = 0; i < buffer.length; i++)
        data[len + i] = buffer[i];

    if( buffer_stream_done()) 
        callback( new Uint8Array(data));
}

大多数 Javamart 引擎将已经为动态分配的内存预留一些空间。 这个方法将使用该空间, 而不是创建许多新的内存分配, 这可能是操作系统内核内核内的一种性能杀手。 除此之外, 您还会刮掉一些功能电话 。

A second, more involved option would be to allocate the memory beforehand. If you know the maximum size of any data stream then you could create an array buffer of that size, fill it up (partially if necessary) then empty it when done.

最后,如果性能是您的首要目标, 而且您知道最大包包大小( 而不是整个流), 那么从这个大小的数组缓冲开始。 当你填充您预分配的内存时, 在网络调用之间创建新的缓冲 -- 如果可能的话, 将不同步 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.