static constant vs jsonData pros and cons

Last week in some thread there was a discussion that involved jsonData. So I tried it and I was positively surprised by it's memory gains!

My static code looked like:

typedef ValueType as Numeric or Time.Duration or String or Null or Array<Numeric or Time.Duration or String>;
const DATA = [/*lots of data*/] as Array<ValueType>;

Then I changed it by removing the data from the code and added it as jsonData:

<resources>
    <jsonData id="data" filename="data.json"></jsonData>
</resources>

var DATA as Array<ValueType> = [] as Array<ValueType>;

DATA = Application.loadResource(Rez.JsonData.data) as Array<ValueType>;

And I looked in the memory viewer in both cases.

There's a gain in: code size (obvious), but also in data size (surprising), and most importantly in peak memory. However the most surprising is that even the globals.DATA size shrinked, even though the data looks the same. Some of the elements became smaller when loaded via loadResource.

  • Ok, so I made a mistake above: some of the elements became BIGGER when loaded via loadResource. For example a string: "string" which contains 6 ASCII characters is 15 bytes when it's in static code but 16 bytes when loaded via loadResource. Like the overhead of the String object is different when it's a static constant (+9) vs when loaded from jsonData (+10).

    But still the gain of jsonData is worth it,

  • I haven’t checked this for a long time, but when I experimented, I discovered that in addition to data being in memory, memory was also wasted on storing program code. When storing data in JSON, no program memory is used. I used SIM for analysis.

  • that's exactly what I wrote above Slight smile