Stephen
Stephen

Reputation: 5470

Increasing performance of loops

I'm currently working on a script that helps to manage connections to phone numbers with different things like contacts, text messages, etc. The big problem with phone numbers in our data is that a phone number can be something like..

13334445555
3334445555
013334445555
444555

All of these would probably be the same number. So, I've got a 'lookupNumber' method on an object that contains all sorts of references (none of which are important for this question), and a hasVariant method on each 'cloud' - a name that I gave to an object that represents one or more variations of a phone number. The 'cloud' could start off with a shorter number than the ones given to it, so it has to be able to vary which number it is trying to place.

The object with all the 'clouds' has a relevant data structure of:

{
  clouds: [], //0-n clouds
  phoneNumberCache: {} //i.e. 333444555: {//ref to cloud}
}

The 'clouds' have the relevant data structure of:

{
  numbers: [] //1-n numbers
}

lookupNumber method:

lookupNumber: function (number) {
  if(this.phoneNumberCache[number] === undefined) {
    var i, l, cloud;
    for (i = 0, l = this.clouds.length; i < l; i++) {
      cloud = this.clouds[i];
      if(cloud.hasVariant(number)) {
        this.phoneNumberCache[number] = cloud;
        return cloud;
      }
    }
    return false;
  } else {
    return this.phoneNumberCache[number];
  }
}

hasVariant method:

hasVariant: function (testNumber) {
  for(var i = 0, l = this.numbers.length; i < l; i++) {
    var number = this.numbers[i], needle, haystack, index, needleLength, haystackLength;

    if(testNumber.length < number.length) {
      needle = testNumber;
      haystack = number;
    } else {
      needle = number;
      haystack = testNumber;
    }
    needleLength = needle.length;
    haystackLength = haystack.length;

    if(needleLength <= this.minimumLength && haystackLength != needleLength) {
      continue;
    }

    index = haystack.indexOf(needle);

    if(index != -1 && (index + needleLength) == haystackLength && (needleLength / haystackLength) > this.minimumMatch) {
      return true;
    }

  }
  return false;
}

On my testing account, the performance is 'meh' on firefox and terrible on IE8 (the minimum IE browser we support). Firebug is reporting the following times for the two methods:

Function        Calls   Percent Own Time    Time        Avg     Min     Max
hasVariant      1355484 32.1%   1653.577ms  1653.577ms  0.001ms 0.001ms 0.205ms 
lookupNumber    2120    31.17%  1605.947ms  3259.524ms  1.538ms 0.001ms 4.736ms

The test data has 1674 unique phone numbers in it - about 450 of the 'lookupNumber' calls are going to return an already cached number.

Maybe I've been staring at the data too long, but it seems the number of times the 'hasVariant' gets called is way too high and they're both such simple methods.. I can't think of any way to make them faster. Because it's still very slow on IE, I'm still looking for a way to squeeze some more performance out of it. Any ideas on how to make this more efficient? Or do I have a bug in here that might be causing this to not cache a number?

Upvotes: 0

Views: 94

Answers (1)

Tikhon Jelvis
Tikhon Jelvis

Reputation: 68172

I have an idea: don't use an array to store the numbers inside of the cloud. Instead, use an object as so:

cloud : {
  numbers : {
    "4445555"    : true,
    "3334445555" : true,
    // Etc
  }
}

This way, when you want to check if a number is in there, just do something like this:

hasVariant : function (number) {
  while (number.length >= 7) {
    if (this.numbers[number]) return true;
    number = number.substring(1);
  }
  return false;
}

I think this should be faster than your version.

Upvotes: 1

Related Questions