Skip to content

JSON Performance

Matan Lurey edited this page Oct 22, 2021 · 2 revisions

From @Larkik on the TTSClub Discord:

Regarding the slow json stuff: JSON is just a Lua library that TTS provides to you. For some reason it is really, really slow. It could be because it sorts the string keys for every object but I'm not sure. It being implemented in pure Lua is also not optimal but that should not be a super huge issue. That should be its source code: https://github.com/Berserk-Games/Tabletop-Simulator-Lua-Classes/blob/master/JSON/JSON.lua

In theory you can use any other json library. You would just need to copy the code to a file in your project. I used (a slightly modified version of) this one for a while for example https://github.com/rxi/json.lua

Then there is also the built-in module json (written all lower case.) That one is provided by Moonsharp (Lua Interpreter for C# that TTS uses) and is therefore written in C#. You can find its documentation here: https://www.moonsharp.org/additions.html#the-json-module

I benchmarked all three of them some time ago be deserializing a huge json object. Moonsharps json needed 0.03 seconds, rxi json 1.5 seconds and TTS JSON was done after 29 seconds. As you can see, TTS JSON is really, really bad if you need to parse complex json objects and in such cases you should probably use something else.

However, all three of them have different issues with parsing non-ascii characters or escaped unicode sequences which you have to work around:

function module.convertUnicodeSequences(str)
    local pattern = [[\u[0-9a-fA-F]+]]
    local f = function(s) return string.sub(s, 1, 2) .. "{" .. string.sub(s, 3, 6) .. "}" .. string.sub(s, 7) end
    return string.gsub(str, pattern, f)
end

Clone this wiki locally