From 31c6a32d2301333e6b263d23e970aed6f89533e2 Mon Sep 17 00:00:00 2001 From: Kirk Scheibelhut Date: Tue, 19 Feb 2019 17:05:41 -0800 Subject: [PATCH] Optimize Dex#getEffect by caching the result. As outlined by pkmn.cc/optimize, this change should result in a ~30% increase in Battle performance over repeated runs. Caching the resultant effect is safe provided nothing mutates it. --- sim/dex.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/sim/dex.js b/sim/dex.js index 47ffd71e5ac0c..de1482fbc80c7 100644 --- a/sim/dex.js +++ b/sim/dex.js @@ -130,6 +130,8 @@ class ModdedDex { this.abilityCache = new Map(); /** @type {Map} */ this.typeCache = new Map(); + /** @type {Map} */ + this.effectCache = new Map(); if (!isOriginal) { const original = dexes['base'].mod(mod).includeData(); @@ -458,10 +460,8 @@ class ModdedDex { moveCopy.hit = 0; return moveCopy; } + /** - * While this function can technically return any kind of effect at - * all, that's not a feature TypeScript needs to know about. - * * @param {?string | Effect} [name] * @return {PureEffect} */ @@ -473,6 +473,22 @@ class ModdedDex { // @ts-ignore return name; } + + let cached = this.effectCache.get(name); + if (!cached) { + cached = this.getEffectInner(name); + this.effectCache.set(name, cached); + } + return cached; + } + + /** + * While this function can technically return any kind of effect at + * all, that's not a feature TypeScript needs to know about. + * @param {string} name + * @return {PureEffect} + */ + getEffectInner(name) { if (name.startsWith('move:')) { // @ts-ignore return this.getMove(name.slice(5));