Sorry, we don't support your browser.  Install a modern browser

[2.0 beta] JSON.encode is very slow on tables with large number keys#3277

JSON.encode writes a table with a number key as an array, padding every slot below that key with null. The time this takes grows with the square of the key, so a large key looks like a freeze: TTS stops responding and has to be force-quit.

Environment: TTS 2.0 public beta (Steam build 27134291, 2026-12-03); Unity 6000.7.0b9; Linux desktop; singleplayer host.

Reproduce (Global):

local t0 = os.clock()
local s = JSON.encode({[20000] = 1})
print(#s, os.clock() - t0)  --> 100000 bytes, about 7 seconds

Measured, encoding a table with one entry:

KeyOutputTime
15 bytes0.1 ms
49245 bytes0.2 ms
10005,000 bytes15.7 ms
500025,000 bytes457 ms
20000100,000 bytes7.1 s

How we hit it: JSON.encode(obj.getData()) on any card lying on the table. The card’s CustomDeck is keyed by its deck id (ours: 644533), so the encoder pads 644,532 nulls: about 2 hours and 3 MB of output at the rate above. The same card’s data taken from a deck’s getData().ContainedObjects encodes in 3.5 ms, because there its key was 49.

Expected: milliseconds, and a sparse table written as an object ({"644533": {...}}), as obj.getJSON() writes it.

Actual: the main thread is busy for hours building an array of nulls.

Workaround: obj.getJSON() returns the same card as JSON in 1 ms. For a mod’s own tables, use string keys.

Not checked: the current release (v14). The null padding may be long-standing; we can’t say whether the slowdown is new in 2.0.

Found while benchmarking a move of our card buttons to VisualUI.

a day ago
Changed the status to
Awaiting Release
11 hours ago