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..


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) {

    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?


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.

Need Your Help

How to allocate from heap with the correct memory alignment for InterlockedIncrement function?

c++ multithreading thread-safety heap memory-alignment

This code seems to work, but have I used the InterlockedIncrement function correctly? The correct memory alignment of m_count is of my primary concern. Assume we're on a x86-64 system and compile a

extract some html with preg_match

php html arrays preg-match extract

i use preg_mach for extract some html ( i try to use DOMDocument but i had some problem with new line )

How do I load a HTML table sections at a time

jquery html ajax table twitter

I have a table that will rebuild async everytime a new option is selected from a dropdown. I am able to make the call and the HTML is generated correctly but when sending it back across the wire it

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.