I’ve probably seen many developers saying that using buffer will help you lower data size especially when using remote event to transfer data.
But are they really?
As a far I know buffers are a very primitive type of data storage, down to the individual bits of data. For which they say that if used properly they can reduce data passing significantly. But when using a very simple set of data (eg just a array of integers) is compressing still useful?
Just passing a simple set of data,
-- a remote event (assume)
local Event
-- normal table with integers
local tab = {0,1,20,7}
-- a buffer with the same integers as 8-bit unsigned shorts (assume)
local buf
-- Sending the data
Event:FireServer(tab)
Event:FireServer(bug)
I assume in this case Roblox may compress the table to pass. But is it better to use buffers?
First of all, you seem to missunderstand the concept a bit. You can check my guide to understand it, but I will summarize it here anyway
Buffers, unlike tables, do not have a constant size. So a float (Lua number) would be 8 bytes (64 bits). While short would be only 2 bytes (16 bits). In this case, you transport a table of 4 Lua numbers. Which is 32 bytes in total. While if you were to transport 4 numbers of the short data type, you would only transport 8 bytes.
It might seem like buffers are superior in all cases. But the thing is when you pass a table through the remote event (either unreliable or not). It will be converted into a JSON table. It will vary in size, but usually it will match the amount of characters the Lua table was created with. So {1,5,1,20} will be 10 characters; that equals to 10 bytes
10 bytes from the table solution and 8 bytes from the buffer solution is not a lot. It will only make a difference if you work with huge numbers.
In conclusion, buffers should be used if you need to send huge amounts of data in a compact way (like in unreliable remote events that have a hard limit of 900 bytes). You really shouldn’t remove the convenience of easy table read/write over negligible in most cases memory optimization
I’d also add that buffers are mostly useful when you know the limitations of the data you’re sending over. For example how many different states it can be, or within what range, etc.
Do not use buffers, they take a lot of time to read and write from without native code generation being enabled.
It’s much better to use simple data types, unless you are limited to the amount of data you can send, such as for example when using an UnreliableRemoteEvent