Skip to content Skip to sidebar Skip to footer

Javascript: Smallest Json.stringify For Float32array?

FireFox 46.0.1: I am using 3rd-party (easyrtc) software to send 15KB chunks of Float32Arrays between peers. Easyrtc insists that the data be JSON-able. Unfortunately, JSON.strin

Solution 1:

Yes

ES6: Assume that your data are in let f32 = g_testBufferView (array Float32Array) ) - whe can save it as JSON array in at leas 4 ways:

// code let f32json = JSON.stringify(f32);
let f32jsonArr = JSON.stringify(Array.from(f32));
let f32base64 = btoa(String.fromCharCode(...(newUint8Array(f32.buffer))));
let f32base128 = ... // not trivial, look below// decodelet df32json = newFloat32Array(Object.values(JSON.parse(f32json))); 
let df32jsonArr = newFloat32Array(JSON.parse(f32jsonArr));
let df32base64 = newFloat32Array(newUint8Array([...atob(f32base64)].map(c => c.charCodeAt(0))).buffer);
let df32base128 = ... // not trivial, look below

Note that Object.values return values sorted by numeric keys (look here).

Here is working example. You can also use base128 do decode but I not use in this example (to not complicate it) - more details here.

If your Float32Array- f32 has 4096 elements equals to 0.3 then:

  • f32 has 16384 bytes,
  • f32json (j from your question) has 109483 bytes (which is >6x bigger than f32)
  • f32jsonArr has 81921 bytes (which is >5x bigger than f32)
  • f32base64 has 21848 bytes(which is ~1.3x bigger than f32)
  • f32base128 has 18725 bytes (whis is <1.15x bigger than f32) but chrome will send ~2x bigger request (depends on input data)

If your Float32Array- f32 has 4096 elements equals integer from 1 to 9 then:

  • f32 has 16384 bytes - CONST,
  • f32json (j from your question) has 35755 bytes (which is >2x bigger than f32)
  • f32jsonArr has 8193 bytes (which is 2x SMALLER (sic!) than f32)
  • f32base64 has 21848 bytes - CONST (which is ~1.3x bigger than f32)
  • f32base128 has 18725 bytes - CONST (whis is <1.15x bigger than f32) but chrome will send ~2x bigger request (depends on input data)

Conclusion

The smallest result which not depends of array values (result size is constant) we get for f32base64 ~33% bigger than input array size. For f32base128 - it contains valid JSON (string) which is something about <15% bigger than input, but chrome during sending increase this size (look here - on 'update' section). So use f32base64 - this is probably the smallest JSON that you can get without more sophisticated methods.

Post a Comment for "Javascript: Smallest Json.stringify For Float32array?"