2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-06 10:50:21 +08:00

optimize rule lookup

we have tons of bbcode rules, might as well speed them up
This commit is contained in:
Sam 2017-06-30 15:19:07 -04:00
parent 0ba39109a0
commit 0650c8dbab
3 changed files with 23 additions and 23 deletions

View file

@ -48,8 +48,24 @@ class Ruler {
return this.rules;
}
getRuleForTag(tag) {
this.ensureCache();
return this.cache[tag];
}
ensureCache() {
if (this.cache) { return; }
this.cache = {};
for(let i=this.rules.length-1;i>=0;i--) {
let info = this.rules[i];
this.cache[info.rule.tag] = info;
}
}
push(name, rule) {
this.rules.push({name, rule});
this.cache = null;
}
}