My keyword-value pair search code is screaming fast but we can make it faster. Here's the code at Fiddle.
When a client calls HashCompactor's .add(keyword,datum) method, it feeds the keyword through a preprocessor which is used to iterate through the keyword. In this case, just assume character is a one-letter string. All keywords beginning with the letter 'a' will found from that node's children, like a tree.
For this question, what's the fastest way to reuse _finder3 and _finder4 to create a _finderN function? You can see from the code that there's a pattern to a _finderN routine, but I'm not sure how best to automate that process.
_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;
}
},