What better: use string manipulations to save data, or use tables and encode them with JSON?

Hello guys. I’m trying to make saving system. I’m trying to save as much things as possible with roblox DataStores, but also not forgetting about loading speed. I know that there’s JSONEncode and Decode exist, and it convert any table/dictionary to string. But also, I know that I can make smth like this myself, with strings.
Some examples what I can achieve with JSON and String:
String:

Example of saved string:
"34F|10,8,-5,1,0,0,0,1,0,0,0,1;1;1,100;21,10;5,16|3,51;81,52|;"
ID_of_item|CFrame;Prop Level;n-materials and their amount in pairs|additional properties|;
--additional properties are stored inside item, or time for things like furnace
-ClassName in this example is Model which can be in ReplicatedStorage

(Pretty hard to understand string format, but cursed things like this can greatelly decrease memory usage, because single character uses 8 bit. And converting items to ID will decrease character amount)
JSON:

Example of saved JSON:
'{"341":[[10,8,-5,1,0,0,0,1,0,0,0,1,1,1,100,21,10,5,16],[3,51,81,52]]}'
Almost same things as above, but encoded with JSON.

As you can see here, Strings will be shorter. But IDK how much time they will spend. Strings will need:

  1. Convert prop to it’s ID
  2. Convert prop ID to string
  3. Find in some array what properties should be saved
  4. Convert needed properties to string
  5. Convert additional properties to string, if they exist
  6. Concatenate everything together with separators |

JSON will need:

  1. Convert prop to it’s ID
  2. Find params which need be saved
  3. Find additional properties, if they exist
  4. Put to dictionary: [ID]={{Params}, {AdditionalParams}}
  5. Encode dictionary with JSON

So, I got question:

What will be faster:

  1. String manipulations
  2. JSON usage

It really depends on your game. Use string manipulation if storage space matters a lot (you can compress it); the game will be saving a lot of data and it can’t risk wasting any of it. Use JSON if storage space isn’t a concern but performance is; you don’t want to wait a while to compress/decompress the data with a complicated algorithm.

If you’re asking for an opinion, I would rather waste a tiny bit of performance on compressing data by several folds. It also improves bandwidth if that’s a concern. Depending on your game you should look for new methods of compacting data; for example, is it really necessary to store the entire CFrame, or can you just store an ID representing a position and a rotation?

Below is an example of data compression from my own project, just to show how crazy it can get. It uses a combination of LZW compression and base93 encoding with minimal performance overhead. Perhaps you can do something similar with your game
ScreenShot_20220712160718

1 Like