Sugarcube 2 Memory Usage

What causes Sugarcube 2 to use RAM? Specifically, I’ve heard that there are situations where a lot of passage instances get created in the DOM. I don’t expect the State to be a memory issue (unless I’m severely under-informed), but I’d like to know what to avoid or watch out for, when reducing memory load.

I would say State was more likely to cause excessive memory usage than passage numbers.

Passages are stored as HTML elements, specifically <tw-passage>, so the more passages, the more such elements. That will inevitably make for a larger DOM, and DOM processing does indeed require more browser memory, but I think the number of elements in even a large game (100s, maybe 1000+) is still way lower than the number of DOM elements involved in a single YouTube (or similar) page. And of course browsers are optimised for DOM processing. For that reason I wouldn’t expect it to have a huge impact.

State, however, can balloon if you have a long game-state history, and lots of data in Story variables, because the storage required is multiplied by the number of past moments. If you stick 1k of data into a story variable, and have 40 past moments available in the game history, then you are using 40k of data (roughly — past data is compressed, so it’s not as linear as that). If you start putting a lot of data into State it can ramp up, and of course that data lives on the browser’s JS thread, not in the DOM, which may or may not be as optimised.

Honestly, though, I’ve never seen a Twine game where RAM usage has been a concern. There are other pain points:

  • The number of passages can cause issues in the Twine editor, as it can struggle to draw the passage map. This was a bigger issue in past versions.
  • The size of State, and number of past moments, can cause slowdowns in passage transitions, as the new moment is converted to a past one and compressed into the session state. Again, this has been mitigated in newer versions of SugarCube
  • The size of State, and number of past moments, can swell the size of saved games, and cause some browsers to run out of the pretty parsimonious storage allocations they give for local storage (5Mb or less)

Generally, however, most games will not ever see any of these issues.

3 Likes