javascript:Float32Array的最小JSON.stringify? [英] javascript: smallest JSON.stringify for Float32Array?
问题描述
FireFox 46.0.1:我正在使用第三方(easyrtc)软件在对等体之间发送15KB的Float32Array块。 Easyrtc坚持认为数据是JSON能力的。不幸的是,JSON.stringify产生的字符串是原始数据的两倍多:16384字节的数据变为长度为35755的字符串。下面是我的测试代码,后跟控制台输出。如果可以做任何事情来减少stringify'd大小怎么办?有没有办法只发送值(没有键)?我可以使用'replacer'参数只发送值,如果是,我不需要在接收端的相应JSON.parse上使用替换器吗?
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.stringify yields a string more than twice as long as the original data: 16384 bytes of data becomes a string of length 35755. Below is my test code followed by the console output. What if anything can I do to reduce the stringify'd size? Is there a way to send the values only (no keys)? Can I use the 'replacer' argument to send only the values, and if so, don't I need to use a replacer on the corresponding JSON.parse on the receiving end?
var g_testBufferNBytes = 4096 * 4;
var g_testBuffer = new ArrayBuffer(g_testBufferNBytes);
var g_testBufferView = new Float32Array(g_testBuffer);
console.log("array byte length " + g_testBuffer.byteLength);
console.log("view byte length " + g_testBufferView.byteLength);
var j = JSON.stringify(g_testBufferView);
console.log("j length " + j.length);
var newBuf = JSON.parse(j);
console.log("newBuf length " + Object.keys(newBuf).length);
CONSOLE:
数组字节长度16384
查看字节长度16384
j长度35755
newBuf长度4096
CONSOLE: array byte length 16384 view byte length 16384 j length 35755 newBuf length 4096
推荐答案
是
ES6 :假设您的数据在中,让f32 = g_testBufferView
(数组 Float32Array
) ) - whe可以以4种方式将其保存为JSON数组:
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(...(new Uint8Array(f32.buffer))));
let f32base128 = ... // not trivial, look below
// decode
let df32json = new Float32Array(Object.values(JSON.parse(f32json)));
let df32jsonArr = new Float32Array(JSON.parse(f32jsonArr));
let df32base64 = new Float32Array(new Uint8Array([...atob(f32base64)].map(c => c.charCodeAt(0))).buffer);
let df32base128 = ... // not trivial, look below
注意 Object.values
返回按数字键排序的值(查看这里)。
Note that Object.values
return values sorted by numeric keys (look here).
这是工作示例。您也可以使用 base128 进行解码,但我不在此示例中使用(不要使其复杂化) - 更多细节这里。
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.
如果您的 Float32Array
- f32
有4096个元素等于到 0.3
然后:
If your Float32Array
- f32
has 4096 elements equals to 0.3
then:
-
f32
有16384字节, -
f32json
(j
来自你的问题)有109483字节(比f32
大6倍) -
f32jsonArr
有81921个字节(比f32
大5倍) -
f32base64
有21848个字节(比f32
大1.3倍) -
f32base128
有18725个字节(whis比f32
大1.15倍)但chrome会发送大约2倍的请求(取决于输入数据)
f32
has 16384 bytes,f32json
(j
from your question) has 109483 bytes (which is >6x bigger thanf32
)f32jsonArr
has 81921 bytes (which is >5x bigger thanf32
)f32base64
has 21848 bytes(which is ~1.3x bigger thanf32
)f32base128
has 18725 bytes (whis is <1.15x bigger thanf32
) but chrome will send ~2x bigger request (depends on input data)
如果你的 Float32Array
- f32
有4096个元素等于1到9之间的整数:
If your Float32Array
- f32
has 4096 elements equals integer from 1 to 9 then:
-
f32
有16384字节 - CONST , -
f32json
(j
来自您的问题)有35755个字节(其中比f32
大2倍> -
f32jsonArr
有8193个字节(其中是2x SMALLER (原文如此!)比f32
) -
f32base64
有21848字节 - CONST (比f32
大1.3倍) -
f32base128
有18725个字节 - CONST (低于< 1.15x大于f32
)但是chrome会发送大约2倍的请求(取决于输入数据)
f32
has 16384 bytes - CONST,f32json
(j
from your question) has 35755 bytes (which is >2x bigger thanf32
)f32jsonArr
has 8193 bytes (which is 2x SMALLER (sic!) thanf32
)f32base64
has 21848 bytes - CONST (which is ~1.3x bigger thanf32
)f32base128
has 18725 bytes - CONST (whis is <1.15x bigger thanf32
) but chrome will send ~2x bigger request (depends on input data)
不依赖于数组值的最小结果(结果大小是常数)我们得到 f32base64
比输入数组大小大33%。对于 f32base128
- 它包含有效的JSON(字符串),大约比输入大15%,但发送期间的chrome增加了这个大小(查看此处 - 在更新部分)。所以使用 f32base64
- 这可能是没有更复杂方法的最小JSON。
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.
这篇关于javascript:Float32Array的最小JSON.stringify?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!