如何存储在Javascript中的字节数组 [英] How to store a byte array in Javascript
问题描述
我要被存储在Javascript大阵字节值(最有可能超过一百万)。如果使用正常的数字的普通阵列,这将需要8 MB的,因为数字存储为IEEE加倍,但如果我可以将其存储为字节,这将是仅1 MB。
I'm going to be storing a large array of byte values (most likely over a million) in Javascript. If I use a normal array with normal numbers, that will take 8 MB, because numbers are stored as IEEE doubles, but if I can store it as bytes, it will be only 1 MB.
我想避免浪费那么多空间,原因显而易见。是否有存储字节字节而不是双打的方法吗?浏览器的兼容性不是我的问题,只要它工作在Chrome中。这是HTML5,如果有差别。
I'd like to avoid wasting that much space for obvious reasons. Is there a way to store bytes as bytes instead of doubles? Browser compatibility isn't an issue for me, as long as it works in Chrome. This is in HTML5, if that makes a difference.
推荐答案
使用类型数组的,您可存储的<一个数组href=\"https://developer.mozilla.org/en-US/docs/JavaScript_typed_arrays/ArrayBufferView#Typed_array_subclasses\">these类型的:
By using typed arrays, you can store arrays of these types:
- INT8
- UINT8
- 的Int16
- 标记Uint16
- 的Int32
- UINT32
- 浮点32
- Float64
例如:
var array = new Uint8Array(100);
array[42] = 10;
alert(array[42]);
看到它在行动这里。
这篇关于如何存储在Javascript中的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!