Help with table size

I am trying to look for a way to find the size of a table (in bytes). Does anyone know how I could do this?

Thanks :slight_smile:

function tableSizeInBytes(tbl)
    local totalSize = 0
    local function sizeOf(obj)
        local dataType = type(obj)
        if dataType == "number" then
            return 8
        elseif dataType == "string" then
            return #obj
        elseif dataType == "boolean" then
            return 1
        elseif dataType == "table" then
            return tableSizeInBytes(obj)
            return 0

    for key, value in pairs(tbl) do
        totalSize = totalSize + sizeOf(key) + sizeOf(value)

    return totalSize

local myTable = {x = 10, y = 20, z = "Hello, Roblox!"}
local size = tableSizeInBytes(myTable)
print("Table size in bytes:", size)

This is only going to work for PODs (after including things like vector, and even then this doesn’t include something like a table’s constant memory cost). Even then, this is only going to be an estimate–you’re not including the base cost of a Lua data type, and a string for example is definitely going to be more than just its length.

There’s no way I can think of to get the size in bytes of something like an Instance, it’s all too complicated unfortunately.

return #game.HttpService:JSONEncode(data) * 8

Multiplied by 8 because, characters are made of 8 bytes.

Wouldn’t this be 1? One byte? 1 or 0? Confused.

yes you are right it should be one byte .