pucispeedtest

JavaScript performance comparison

Revision 2 of this test case created

Info

personal test for form validation

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Optimized
$(document).ready(function() {

  var $newsItem = $('#news-item'), $validation = $('#validation'), $newsToggler = $('#news-toggler'), $requestInformation = $('#request-information');

  $newsItem.hide();
  $validation.hide();

  $newsToggler.on('click', function() {
    $newsItem.fadeToggle('slow');
  })

  /* form validation */

  $('#request-information').submit(function() {
    var name = $('#name').val();
    var emailAddress = $('#email-address').val();
    var phoneNumber = $('#phone-number').val();

    /* regular expression for email and phone number validation test */
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var numberReg = /^(0|[1-9][0-9]*)$/;
    var message = '<span class="message">invalid value</span>';

    var isValid = true;

    // check for required fields and data validity
    $('.message').remove();
    if (name == '' || name == 'Your name') {
      isValid = false;
    }
    if (emailAddress == '' || emailAddress == 'Email address') {
      isValid = false;
    } else if (!emailReg.test(emailAddress)) {
      $('#email-address').parent().append(message);
      isValid = false;
    }
    if (phoneNumber == '' || phoneNumber == 'Phone Number') {
      isValid = false;
    } else if (!numberReg.test(phoneNumber)) {
      $('#phone-number').parent().append(message);
      isValid = false;
    }
    // if form not valid
    if (!isValid) {
      $('#validation').slideDown();
      return false;
    }
  });

});
pending…
Nonoptimized
$(document).ready(function() {

  $('#news-item').hide();
  $('#validation').hide();

  $('#news-toggler').click(function() {
    $('#news-item').fadeToggle('slow');
  });

  /* form validation */

  $('#request-information').submit(function() {
    var name = $('#name').val();
    var emailAddress = $('#email-address').val();
    var phoneNumber = $('#phone-number').val();

    /* regular expression for email and phone number validation test */
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var numberReg = /^(0|[1-9][0-9]*)$/;
    var message = '<span class="message">invalid value</span>';

    var isValid = true;

    // check for required fields and data validity
    $('.message').remove();
    if (name == '' || name == 'Your name') {
      isValid = false;
    }
    if (emailAddress == '' || emailAddress == 'Email address') {
      isValid = false;
    } else if (!emailReg.test(emailAddress)) {
      $('#email-address').parent().append(message);
      isValid = false;
    }
    if (phoneNumber == '' || phoneNumber == 'Phone Number') {
      isValid = false;
    } else if (!numberReg.test(phoneNumber)) {
      $('#phone-number').parent().append(message);
      isValid = false;
    }
    // if form not valid
    if (!isValid) {
      $('#validation').slideDown();
      return false;
    }
  });

});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment