I want to better write this JavaScript procedure

In summary, to improve the writing of a JavaScript procedure, it is important to follow good coding practices such as using meaningful variable names, commenting code, and avoiding unnecessary repetition. Additionally, breaking down the procedure into smaller, more manageable functions can also improve readability and efficiency. It is also helpful to test the code thoroughly and make use of built-in JavaScript functions and libraries to simplify tasks. By implementing these techniques, the resulting JavaScript procedure will be easier to understand and maintain.
  • #1
Jamin2112
986
12
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:
		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;
		}
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
AlephZero said:
Adding characters to the string "thisword" one at a time could be slow

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
  • #8
Jamin2112 said:
That accomplishes the same task, but why is it more efficient than string addition? Is the implementation different?

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.
 

1. What are the basic components of a JavaScript procedure?

A JavaScript procedure is made up of a name, a list of parameters, and a body of code that will be executed when the procedure is called.

2. How do I declare a JavaScript procedure?

To declare a JavaScript procedure, use the keyword "function" followed by the name of the procedure, a set of parentheses for the parameters, and curly braces for the body of the code. For example: function myProcedure(param1, param2) { // body of code }

3. Can I use variables in my JavaScript procedure?

Yes, you can use variables in your JavaScript procedure to store and manipulate data. However, make sure to declare them within the procedure so they are only accessible within the scope of the procedure.

4. How do I call a JavaScript procedure?

To call a JavaScript procedure, use the name of the procedure followed by a set of parentheses with any necessary arguments. For example: myProcedure(arg1, arg2)

5. How can I make my JavaScript procedures more efficient?

To make your JavaScript procedures more efficient, make sure to use local variables instead of global variables, avoid repetitive code, and follow best practices such as using meaningful names for variables and comments to explain your code. You can also use built-in functions and methods to simplify your code and improve performance.

Similar threads

  • Programming and Computer Science
Replies
1
Views
910
  • Programming and Computer Science
Replies
6
Views
975
  • Programming and Computer Science
Replies
4
Views
742
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
Replies
1
Views
869
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top