Skip to content

Commit

Permalink
Refactor getFormat pattern and ordering for speed (#5194)
Browse files Browse the repository at this point in the history
Even though 31c6a32 added caching to make this method less of a
hotspot, there's no reason to use a pattern which is an order of
magnitude slower (https://jsperf.com/pokemon-showdown-getEffect).
Also, rearranges Format to get checked first as pkmn.cc/optimize
finds it to be the most frequently accessed.
  • Loading branch information
scheibo authored and KamilaBorowska committed Feb 23, 2019
1 parent 6266a3b commit 9ce0066
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions sim/dex.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,19 +495,18 @@ class ModdedDex {
}

let id = toId(name);
if (this.data.Statuses.hasOwnProperty(id)) {
effect = new Data.PureEffect({name}, this.data.Statuses[id]);
} else if (this.data.Movedex.hasOwnProperty(id) && this.data.Movedex[id].effect) {
name = this.data.Movedex[id].name || name;
effect = new Data.PureEffect({name}, this.data.Movedex[id].effect);
} else if (this.data.Abilities.hasOwnProperty(id) && this.data.Abilities[id].effect) {
name = this.data.Abilities[id].name || name;
effect = new Data.PureEffect({name}, this.data.Abilities[id].effect);
} else if (this.data.Items.hasOwnProperty(id) && this.data.Items[id].effect) {
name = this.data.Items[id].name || name;
effect = new Data.PureEffect({name}, this.data.Items[id].effect);
} else if (this.data.Formats.hasOwnProperty(id)) {
effect = new Data.Format({name}, this.data.Formats[id]);

let found;
if ((found = this.data.Formats[id])) {
effect = new Data.Format({name}, found);
} else if ((found = this.data.Statuses[id])) {
effect = new Data.PureEffect({name}, found);
} else if ((found = this.data.Movedex[id]) && found.effect) {
effect = new Data.PureEffect({name: found.name || name}, found.effect);
} else if ((found = this.data.Abilities[id]) && found.effect) {
effect = new Data.PureEffect({name: found.name || name}, found.effect);
} else if ((found = this.data.Items[id]) && found.effect) {
effect = new Data.PureEffect({name: found.name || name}, found.effect);
} else if (id === 'recoil') {
effect = new Data.PureEffect({name: 'Recoil', effectType: 'Recoil'});
} else if (id === 'drain') {
Expand Down

0 comments on commit 9ce0066

Please sign in to comment.