I'm just wondering why everyone uses ArrayBuffer
instead of just a normal array
, string
or stringified JSON
for sending messages from the server to the client. Is it more efficient?
我只是想知道为什么每个人都使用ArrayBuffer而不仅仅是一个普通的数组,字符串或字符串化的JSON,用于从服务器向客户端发送消息。它效率更高吗?
Also, just wondering what Uint8Array
is, how it is different, where to use the two etc.
另外,只是想知道Uint8Array是什么,它是如何不同,在哪里使用两个等。
I am currently using Node.js with Socket.io, but I am happy to change to pure WebSocket
s if it is a better approach.
我目前正在使用带有Socket.io的Node.js,但如果它是一种更好的方法,我很乐意改为纯WebSockets。
1 个解决方案
#1
8
An ArrayBuffer
is more than just a simple array. It contains raw binary data. This is very useful for direct memory manipulation and conserving space.
ArrayBuffer不仅仅是一个简单的数组。它包含原始二进制数据。这对于直接内存操作和节省空间非常有用。
When you create a normal array, you won't get a proper set of contiguous memory in many cases since arrays can contain any combination of different kinds of objects. This is useful when you have dynamic data and multiple types together (frequently happens in JS) but is not so useful when you know the exact layout of memory that you need.
当您创建普通数组时,在许多情况下您将无法获得一组适当的连续内存,因为数组可以包含不同类型对象的任意组合。当您将动态数据和多个类型放在一起时(通常在JS中发生),这很有用,但在您知道所需的内存的确切布局时则不太有用。
This also allows you to view the data at the byte level. For example, it's pretty common in binary data formats to have a n byte long identifier number, an m byte long field telling you how many bytes are used for this field, and m' bytes of data that actually makes up the data field.
这也允许您在字节级别查看数据。例如,在二进制数据格式中,通常有一个n字节长的标识符号,一个m字节长的字段,告诉你这个字段使用了多少字节,以及实际构成数据字段的m'字节数据。
[ identifier ][ bytes of data ][ data ]
With an ArrayBuffer, you have the option of moving through that data on the byte level by using various Views. A regular array doesn't allow you to move through the data with that level of granularity because no guarantees are made about the memory layout.
使用ArrayBuffer,您可以选择使用各种视图在字节级别上移动该数据。常规数组不允许您以该粒度级别移动数据,因为不保证内存布局。
Finally, because you are telling the compiler/interpreter exactly how much space you're going to use and exactly how you're going to view it, it can do much more advanced optimizations when working with that data. When iterating through that data, it doesn't have to make calculated leaps through memory. Instead, it knows exactly how far to move ahead in memory to find the next data point.
最后,因为您正在告诉编译器/解释器您将要使用多少空间以及您将如何查看它,所以在处理该数据时它可以进行更高级的优化。在遍历该数据时,它不必通过内存进行计算跳跃。相反,它确切知道在内存中向前移动多远以找到下一个数据点。
As for what Uint8Array
is, it's a typed array. Essentially, it tells the compiler/interpreter that you will be accessing this data exclusively as 8-bit uint
s which, again, allows it to make better optimizations. Then you can use standard array indexing on it (arr[0], arr[1], etc.
) and you'll be getting back the equivalent uint
values out of the array.
至于Uint8Array是什么,它是一个类型化的数组。本质上,它告诉编译器/解释器您将仅以8位uint的形式访问此数据,这再次允许它进行更好的优化。然后你可以在它上面使用标准数组索引(arr [0],arr [1]等),你将从数组中取回等效的uint值。
TL;DR They take less space when the exact data format is known, allows you to move more exactly through your data and gives the compiler/interpreter greater options for optimization.
TL; DR当确切的数据格式已知时,它们占用的空间更少,允许您更精确地移动数据,并为编译器/解释器提供更多的优化选项。
#1
8
An ArrayBuffer
is more than just a simple array. It contains raw binary data. This is very useful for direct memory manipulation and conserving space.
ArrayBuffer不仅仅是一个简单的数组。它包含原始二进制数据。这对于直接内存操作和节省空间非常有用。
When you create a normal array, you won't get a proper set of contiguous memory in many cases since arrays can contain any combination of different kinds of objects. This is useful when you have dynamic data and multiple types together (frequently happens in JS) but is not so useful when you know the exact layout of memory that you need.
当您创建普通数组时,在许多情况下您将无法获得一组适当的连续内存,因为数组可以包含不同类型对象的任意组合。当您将动态数据和多个类型放在一起时(通常在JS中发生),这很有用,但在您知道所需的内存的确切布局时则不太有用。
This also allows you to view the data at the byte level. For example, it's pretty common in binary data formats to have a n byte long identifier number, an m byte long field telling you how many bytes are used for this field, and m' bytes of data that actually makes up the data field.
这也允许您在字节级别查看数据。例如,在二进制数据格式中,通常有一个n字节长的标识符号,一个m字节长的字段,告诉你这个字段使用了多少字节,以及实际构成数据字段的m'字节数据。
[ identifier ][ bytes of data ][ data ]
With an ArrayBuffer, you have the option of moving through that data on the byte level by using various Views. A regular array doesn't allow you to move through the data with that level of granularity because no guarantees are made about the memory layout.
使用ArrayBuffer,您可以选择使用各种视图在字节级别上移动该数据。常规数组不允许您以该粒度级别移动数据,因为不保证内存布局。
Finally, because you are telling the compiler/interpreter exactly how much space you're going to use and exactly how you're going to view it, it can do much more advanced optimizations when working with that data. When iterating through that data, it doesn't have to make calculated leaps through memory. Instead, it knows exactly how far to move ahead in memory to find the next data point.
最后,因为您正在告诉编译器/解释器您将要使用多少空间以及您将如何查看它,所以在处理该数据时它可以进行更高级的优化。在遍历该数据时,它不必通过内存进行计算跳跃。相反,它确切知道在内存中向前移动多远以找到下一个数据点。
As for what Uint8Array
is, it's a typed array. Essentially, it tells the compiler/interpreter that you will be accessing this data exclusively as 8-bit uint
s which, again, allows it to make better optimizations. Then you can use standard array indexing on it (arr[0], arr[1], etc.
) and you'll be getting back the equivalent uint
values out of the array.
至于Uint8Array是什么,它是一个类型化的数组。本质上,它告诉编译器/解释器您将仅以8位uint的形式访问此数据,这再次允许它进行更好的优化。然后你可以在它上面使用标准数组索引(arr [0],arr [1]等),你将从数组中取回等效的uint值。
TL;DR They take less space when the exact data format is known, allows you to move more exactly through your data and gives the compiler/interpreter greater options for optimization.
TL; DR当确切的数据格式已知时,它们占用的空间更少,允许您更精确地移动数据,并为编译器/解释器提供更多的优化选项。