This misconception is often repeated here. You imagine that you can write some quick javascript code to convert your large object into a portable one. But really, any conversion code you write hits the target , as you said. And the more complex the data, the more you lose speed.
Typically, objects (when not transmitted) are converted based on a structured cloning algorithm (which uses the format defined during implementation and is optimal). Any javascript code that you write is more likely to be slower than a structured clone if you achieve the same goal - transferring data as binary files.
The purpose of portable objects is to allow the transfer of binary data, such as images (from the canvas), audio or video. These children of data can be transferred without processing using a structured cloning algorithm, so for some reason a portable interface was added. And the effect is small even for them - see the answer about the transmission speed .
As a final note, I wrote a prototype library that converts javascript objects to and from ArrayBuffer. This is slower, especially for JSON, like data. Benefits (and benefits of any similar code you write):
- You can define custom object conversions
- You can use inheritance (e.g. send your own types like
Foo )
Code to transfer a JSON object as
If your data is like JSON, just stick with a structured clone and don't carry over. If you do not trust me, check it with this code. You will see it slower than usual. postMessage .
var object = {dd:"ddd", sub:{xx:"dd"}, num:666}; var string = JSON.stringify(object); var uint8_array = new TextEncoder(document.characterSet.toLowerCase()).encode(string); var array_buffer = uint8_array.buffer;
The opposite conversion, given that you have an ArrayBuffer :
// Let me just generate some array buffer for the simulation var array_buffer = new Uint8Array([123,34,100,100,34,58,34,100,100,100,34,44,34,115,117,98,34,58,123,34,120,120,34,58,34,100,100,34,125,44,34,110,117,109,34,58,54,54,54,125]).buffer; // Now to the decoding var decoder = new TextDecoder("utf-8"); var view = new DataView(array_buffer, 0, array_buffer.byteLength); var string = decoder.decode(view); var object = JSON.parse(string);