Should I serialise data using tables or a string?

I have a feeling using tables would make the data larger than it would using a string and then parsing it when you want to load it. I’m not quite sure what the limitations are for data stores though.

It’s to store all parts’ respective information on a player’s plot (sandbox-esque game). I know you can’t store some data types like Vector3s and Color3s and what not, so my question really is:

Is serialising it with tables okay?

local data = {
  size = {1, 2, 3}
  position = {1, 1, 1}
  color = {1, 1, 1}
}

Or using a string?

local data = "(3,4,2) (2,2,2) (0,1,0)"

I tried to muster up what I knew about string manipulation already, and got this:

local data = "(3,4,2) (2,2,2) (0,1,0)"
local pattern = "([%d+^%p]+) ([%d+^%p]+) ([%d+^%p]+)"

local function parseData()
  local position, size, color = data:match(pattern)
  
  local parsed = {
    position = position;
    size = size;
    color = color;
  }
  
  for i, data in next, parsed do
    parsed[i] = data:gsub("%,", " ")
    parsed[i] = parsed[i]:gsub("%(", "")
    parsed[i] = parsed[i]:gsub("%)", "")
  end

  for name, data in next, parsed do
    local a, b, c = data:match("(%d+) (%d+) (%d+)")
    print(name:upper() .. " : " .. data)
  end
end

parseData()

Which prints the following:

SIZE : 2 2 2
POSITION : 3 4 2
COLOR : 0 1 0

Take a look at this article: How to save parts, and the idea of Serialization

I hope that answers your question. I do believe the first one is better.

2 Likes

From a first look, there is a small character difference, remember that data is internally JSON’d, so if you were to compare the two forms of serialization:

{size: [1,2,3],position: [1,1,1],color: [1,1,1]}
["(1,2,3) (1,1,1) (0,1,0)"]

So yes it is more efficient, and it’s even more efficient if you’re saving decimals and not just integers, since a simple double like 2.15 enlarges the JSON string a lot since it actually gains way more precision making longer. But the string form of course won’t add more precision.

But as I said before, this almost never matters especially after the new update where datastore service can store up to 1MB of characters, meaning 1 million character. If you start to find yourself quite close to the limit, it might be time to start optimizing a bit, but to be honest I never found myself worrying that much. Also if you serialize them in the form of a string, saving and loading will be more costly, and might make loading slower for large amounts of data, but yet the difference might be negligible.

1 Like