My keyword-value pair search code is lightning-fast, but there's always room for improvement. You can check out the code on Fiddle.
When a client calls HashCompactor's .add(keyword,datum) method, the keyword goes through a preprocessor that iterates through each character of the keyword. Think of it like navigating a tree structure where all keywords starting with the letter 'a' are found in the children nodes.
In this scenario, how can we efficiently reuse _finder3 and _finder4 to create a generalized _finderN function? There seems to be a pattern in the behavior of these functions, but automating the process is still a bit tricky.
_findChild: function(node,character) {
var children = node.children;
if (children) {
var child = children[node.currentChildIndex];
if (child.character === character) {
return child;
} else {
var length = children.length;
if (length > 1) {
switch (length) {
case 2:
return this._finder1(node,node.currentChildIndex ? 0 : 1,character);
case 3:
return this._finder3(node,character);
case 4:
return this._finder4(node,character);
default:
return this._fnNodeFinder.call(this,node,character);
}
}
}
}
return null;
},
_finder1: function(node,index,character) {
var child = node.children[index];
if (child.character === character) {
node.currentChildIndex = index;
return child;
}
return null;
},
_finder3: function(node,character) {
switch (node.currentChildIndex) {
case 0:
return this._finder1(node,1,character)
|| this._finder1(node,2,character);
case 1:
return this._finder1(node,0,character)
|| this._finder1(node,2,character);
case 2:
return this._finder1(node,0,character)
|| this._finder1(node,1,character);
default:
// alert("Invalid _finder3 index of " + index);
return null;
}
},
_finder4: function(node,character) {
switch (node.currentChildIndex) {
case 0:
return this._finder1(node,1,character)
|| this._finder1(node,2,character)
|| this._finder1(node,3,character);
case 1:
return this._finder1(node,0,character)
|| this._finder1(node,2,character)
|| this._finder1(node,3,character);
case 2:
return this._finder1(node,0,character)
|| this._finder1(node,1,character)
|| this._finder1(node,3,character);
case 3:
return this._finder1(node,0,character)
|| this._finder1(node,1,character)
|| this._finder1(node,2,character);
default:
// alert("Invalid _finder4 index of " + index);
return null;
}
},