

The require cache itself is tied to the global cache and gets cleared at the same time, although there are some circumstances where the require cache will get cleared (fully or partially) while the global cache does not.įrom a performance standpoint the fact that the require and global caches clear at once means that global resets are particularly expensive events. This reduces the load on the server and your script as it prevents the various javascript modules from having to be compiled every tick. Require CacheĮvery time require is called the results are cached. For cases where results may change and data gets invalidated meta data- such as a TTL or version identifier- will have to be stored alongside the results to facilitate that. These limitions make the global object ideal for certain types of caching, such as when the result of a function is always going to be the same or if it doesn't matter when "stale" data is used. Placing large amounts of data into the global cache may cause garbage collector to be invoked more frequently and consume more CPU.The global object can not be considered persistent storage. The global object gets reset fairly regular, meaning all of the data will regularly disappear.This is most commonly used to require modules- // executed on new global var mod = require ( 'mod' ) Additionally it allows you to define expensive one-time-run code in the outer scope. The game loop architecture allows you to define a "loop" function which gets run each tick. JSON.parse is run on the Memory string each tick it is accessed, which is more expensive the more data that is stored.įor these reasons it makes sense to limit what is placed in Memory.



Caching is an extremely important element in performance optimization, and Screeps provides it's own unique opportunities and challenges when it comes to cache.Ĭaching is, at it's core, a simple concept- by saving the results of expensive code future calls are less expensive. Screeps is a performance centric game- the better your performance the more you can accomplish in each tick.
