Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I want to better write this JavaScript procedure

  1. May 6, 2014 #1
    As you may have figured out, I'm obsessed with spending exorbitant amounts of time making optimizations to my code. Can anyone help me cut down on the number of comparisons in the following text2words function? The intent of the function should be easy to figure out from the comments.

    Code (Text):

            function is_letter(c)
            {
                /* Returns whether a character c is in the set {'a', 'b', ..., 'z', 'A', 'B', ..., 'Z'}
                */
                return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
               
            }
           
            function text2words(S)
            {
                /* Given a string S, return an array A of all the words
                   in S, a word being defined as a sequence of consecutive
                   characters in the set {'a', 'b', ..., 'z', 'A', 'B', ..., 'Z'}.
                */
                var A = new Array();
                var thisword = new String();
                var i = 0, j = S.length;
                while (i < j)
                {
                   if (is_letter(S[i]))
                   {
                       while (i < j && is_letter(S[i]))
                            thisword += S[i++];
                       A.push(thisword);
                       thisword = "";
                   }
                   else
                   {
                       ++i;
                   }
                }
                return A;
            }
     
     
  2. jcsd
  3. May 6, 2014 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    Intellectually, this kind of optimization can be interesting, but in practical terms, unless this code is going to run on ZILLIONS of records, ever night and needs to be done by 6am, you are wasting time optimizing it, given the speed of modern computers. I used to do exactly the same thing just on general principles 'cause I grew up with computers when they were slow, but I gave it up years ago as a waste of time.
     
  4. May 6, 2014 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    I agree optimizing code can be rather pointless, but the best trick is to learn to write reasonably optimal code first time around. Most programmers of a certain age learned to do that from necessity!

    Adding characters to the string "thisword" one at a time could be slow, and hammer dynamic memory allocation if the implementation isn't very clever. It doesn't complicate the code much to search for the end of the word and then deal with the substring of S[] just once.

    If your isletter() function is only working on 8-bit characters, the quickest way is to set up an array of true/false flags with 256 elements, instead of doing a function call, up to 4 comparisons, and some logical operations.

    If you are reading Unicode text, your isletter function is completely broken anyway. There are of "letters" that are not encoded in 7-bit ascii, even in European languages.
     
  5. May 6, 2014 #4
    What faster way is there of adding characters to the end of a string? JavaScript's string class doesn't have any kind of push() function.
     
  6. May 6, 2014 #5

    Mark44

    Staff: Mentor

  7. May 7, 2014 #6
  8. May 8, 2014 #7
  9. May 8, 2014 #8

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Javascript strings are effectively immutable (I say "effectively" because pedants might jump on a definition of "immutable" which doesn't quite apply to Javascriot.)

    So every time you add one character to a string, you actually create a new string. For short strings, you lose because allocating the new memory area is expensive compared with copying a few bytes of data. For long strings, you lose because you are repeatedly copying large amounts of data, which not only eats up CPU clock cycles, but also eats up the CPU's fast memory cache.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: I want to better write this JavaScript procedure
Loading...