How do I create iterative organic search code? I am parsing site files Log Format

My keyword pair search code is screaming fast, but we can get it done faster. Here's the Fiddle code .

When the client calls the HashCompactor.add (keyword, datum) method, it passes the keyword through the preprocessor, which is used to iterate over the keyword. In this case, just assume that the character is single letter. All keywords starting with the letter "a" will be found in these children node, 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 is a template for the _finderN routine, but I'm not sure how best to automate this 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;
    }
  },

      

+3


source to share


1 answer


what's the fastest way to reuse _finder3 and _finder4 to create a _finderN function?

I suggest a simple for loop that ends before the result is found:

_finderN: function(node, character, n) {
  let currentChildIndex = node.currentChildIndex;
  for (let i = 0; i < n; i++) {
    if (i === currentChildIndex) continue;
    let child = this._finder1(node, i, character);
    if (child) return child;
  }
  return null;
}

      

You will still have to handle the case if you wish default

.

My search code for a key-value pair is screaming fast, but we can do it faster.



Build types generally have superior performance compared to DIY solutions. For performance testing, I recommend using a library like benchmark.js that does warm-ups and averages across many test runs. http://jsperf.com/ is a famous performance testing playground built on the above library.

It looks to me like you are implementing some kind of multimap . This can be achieved using the built-in data type Map

:

// Simple 'multimap' insertion:
let add = (map, key, value) => {
  let data = map.get(key);
  if (data) data.push(value);
  else map.set(key, data = [value]);
  return data; 
}

let map = new Map();
add(map, 'key', 1);
add(map, 'key', 2);
console.log(map.get('key'));
      

Run code


+1


source







All Articles