From 848a7f4e744c8f4dcb4ec88a0e99c4ae6aac25e5 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 6 Jul 2015 11:23:12 +0100 Subject: [PATCH] Optimise getStateQualifier() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Another fix for #1864, this time we’re caching state qualifiers as they are expensive to compute. --- core/modules/widgets/widget.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/core/modules/widgets/widget.js b/core/modules/widgets/widget.js index 53c5ea46b..679433071 100755 --- a/core/modules/widgets/widget.js +++ b/core/modules/widgets/widget.js @@ -195,16 +195,23 @@ Widget.prototype.hasVariable = function(name,value) { Construct a qualifying string based on a hash of concatenating the values of a given variable in the parent chain */ Widget.prototype.getStateQualifier = function(name) { + this.qualifiers = this.qualifiers || Object.create(null); name = name || "transclusion"; - var output = [], - node = this; - while(node && node.parentWidget) { - if($tw.utils.hop(node.parentWidget.variables,name)) { - output.push(node.getVariable(name)); + if(this.qualifiers[name]) { + return this.qualifiers[name]; + } else { + var output = [], + node = this; + while(node && node.parentWidget) { + if($tw.utils.hop(node.parentWidget.variables,name)) { + output.push(node.getVariable(name)); + } + node = node.parentWidget; } - node = node.parentWidget; + var value = $tw.utils.hashString(output.join("")); + this.qualifiers[name] = value; + return value; } - return $tw.utils.hashString(output.join("")); }; /*