Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6892791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:34:44+00:00 2026-05-27T06:34:44+00:00

I’m currently working on a script that helps to manage connections to phone numbers

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:34:45+00:00Added an answer on May 27, 2026 at 6:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.