views:

54

answers:

2

I have a web app using thin clients (bosanova terminals) as the front end for users. I've noticed some performance differences in JavaScript between the thin client and a PC. The terminals are running windows XP embedded with IE6, and the pages I'm referring to are utilizing prototype JS framework to do some rather simple validation on form elements.

for example the following is what I'm using to make sure required fields are populated.
There are two more like this for .numeric and .alaphanumeric that test acordingly and push errors to stop the form form getting submitted.

$$( '.requiredfield' ).each( function ( elem ){
   if ( ( $( elem ).value.length == 0 ) || ( $( elem ).value == null ) ) {
        $( elem ).addClassName( "nonvalid" );
        $( elem ).siblings().first().addClassName( "error" );
        requiredErrors.push( $( elem ) );
   }

});

The problem I'm seeing is a PC in Firefox or IE a form with 5-20 fields on the page takes maybe a half second to process longer than with out the validation. On the Terminal however, it takes anywhere from 15-25 seconds longer to run the validation than the same page with out it. As I believe I've mentioned I've tested this in IE6 on a PC and do not see the performance loss. A call to Bosanova led me to upgrading the memory in the terminal which I did just before this posting and the results hadn't changed.

I can change the script so I loop through the form fields only once and handle .required .numeric .alphanumeric as I go and it will help I'm sure. As it is now there is such a difference in performance between the PC and Terminal (thin client). I'm curious to find out why.

If anyone has any experience troubleshooting or knows why prototype/javascript would suffer such a performance loss on a terminal I'd greatly appreciate some tips.

Update: >>>>>>>>>>>>>>>>>

I have still been testing and looking into this issue and thought I'd share this. We received a newer Terminal yesterday which I loaded up and tested. The new terminal running IE6 worked flawlessly just as any other browser. of course it was a tad slower than a PC because 1. its running IE6 and 2. its a thin client but the speed difference was in the hundreds of a second vs. a 10-50 second difference running the same scripts. The physical specs of the 2 different thin clients are not that different 1.2 ghz(old) vs 1.6 ghz(new) memory was the same and the HD/DOM was 512MB (old) vs 1gig(new). Still have'nt been able to pin point what is going on in the old terminal but looks to be related to that specific model/revision of the terminal.

Update: >>>>>>>>>>>>>>>>>

A: 

I don't know too much about Prototype, but you should cache that element in a local variable instead of wrapping the $ function around it.

$$( '.requiredfield' ).each( function ( elem ){
   var el = $(elem)
   if ( (elem.value.length == 0 ) || elem.value == null ) ) { // is elem.value ever null?
        el.addClassName( "nonvalid" );
        el.siblings().first().addClassName( "error" );
        requiredErrors.push(el);
   }
});

I don't think that will fix all of the performance issues, but maybe it'll shave a few seconds off. I would suggest going ahead with that change you mentioned so it checks all errors/classes in one loop instead of looping through all elements for each type of class.

Maybe something like (again, I don't know prototype well so somethings might be off):

var errors = {};

var rules = { 
   ".required": function (elem) { return elem.value.length == 0; },
   ".alphanumeric": function (elem) { return /[a-zA-Z0-9]+/.test(elem.value);  }
};

$$( "input", "#your_form_id" ).each(function ( elem ) {
   var el = $(elem)
   var classes = (function () { 
      var cls = elem.className.split(' '), classMap = {};
      for (var k in cls) classMap[cls[k]] = true;
      return classMap;
   })(); // get the classes for this element

   for ( var rule in rules ) {
       error[rule] = [];
       if ( rule in classes && !rules[rule](elem) ) {
          el.addClassName("nonvalid");
          el.siblings().first().addClassName("error");
          errors[rule].push(el);
       }
   }

});

Your errors will be in errors.. to access any elements that failed the required rule, you would do errors["required"] which would return an array.

CD Sanchez
thanks for the tips. I'll happily admit I'm still learning JavaScript. I'm a Server side programmer by trade that has been dabbling in Front-end JavaScript over the past year as projects need. I'll rework it a bit in the morning and see if i can make tolerable.
Nathan
just to update. I've added some of your suggestions and did speed the script up some. doing it this way I was able to bypass a lot of logic untill a user makes a mistake. So this helped some (down to 8-15 seconds rather than 15-20). There is still a drastic difference between the PC and thinclent.
Nathan
@Nathan: Have you considered using one of the many validation libraries out there? A lot of them will make your life a lot easier I'm sure. Unless you need some very very custom behavior I'm sure they'll capture all all the features you would need. I actually have my own library that I've been working on for a while.
CD Sanchez
+2  A: 
Pointy
I thought CPU at first also but one of My clients has a terminal with a CPU double the size of my test box. It actually took longer to run a nearly identical page. a LOT longer actually, almost 40 seconds to run the validation across approximately 25 fields.
Nathan
If it were happening to me, I'd probably code up a little timer routine and start narrowing down where the time is being spent. Note that things like adding class names - which triggers a re-layout - can also be really slow in IE6. Other than that, however, I would be quite surprised to learn that the IE6 running in that client were any different from the IE6 on a PC with XP.
Pointy
yup, put a timer in right off the bat. many different forms use this code with various numbers of required / numeric / alphanumeric fields and no single one jumps out as a resource hog. the time taken for each is proportional to the number of that field type in the form, as expected. Just a lot higher on the terminal than anywhere else. I've observed difference in the way the Terminal makes connections ( it opens way more ports ) than a standard PC per connection, but as the browser goes I would have been surprised also.. now I'm starting to wonder =)
Nathan
running portable firefox on the thinclient page times are very quick, not pc quick but blinding speeds compaired to IE6 on the thinclient. so far the tests i've ran show portable firefox on the thinclient running 2-4.5 seconds to complete the ajax request and validation vs the 15-20 i was seeing in IE.
Nathan
Well I wish I could help out - I really appreciate your question because that's some very interesting data I can pack away for future reference.
Pointy
on a side note. would you suggest `$$( 'input.required, textarea.required' ).each( function(){}); ` over ` $$( '.required' ).each();` also?
Nathan
Yes - when you *just* provide a classname pattern, the engine has no choice but to look at every element in the DOM. If you can constrain it, then it can narrow down the search.
Pointy