RegExp complexity (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script src="https://github.com/mikesherov/jquery/raw/10f755b315ceb78399b316084cca390f7e78bcb4/src/css.js"></script>
<script>
  var matchInteger = jQuery.noConflict();
</script>

<script>
  function replace_rnumnopx( rnumnopx ) {
    jQuery.css = function( elem, name, extra ) {
      var ret, hooks;

      // Make sure that we're working with the right name
      name = jQuery.camelCase( name );
      hooks = jQuery.cssHooks[ name ];
      name = jQuery.cssProps[ name ] || name;

      // cssFloat needs a special treatment
      if ( name === "cssFloat" ) {
        name = "float";
      }

      // If a hook was provided get the computed value from there
      if ( hooks && "get" in hooks && (ret = hooks.get( elem, true, extra )) !== undefined ) {
        return ret;

      // Otherwise, if a way to get the computed value exists, use that
      } else if ( curCSS ) {
        return curCSS( elem, name );
      }
    };

    var curCSS = function( elem, name ) {
      var left, rsLeft, uncomputed,
        ret = elem.currentStyle && elem.currentStyle[ name ],
        style = elem.style;

      // Avoid setting ret to empty string here
      // so we don't default to auto
      if ( ret == null && style && (uncomputed = style[ name ]) ) {
        ret = uncomputed;
      }

      // From the awesome hack by Dean Edwards
      // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291

      // If we're not dealing with a regular pixel number
      // but a number that has a weird ending, we need to convert it to pixels
      if ( rnumnopx.test( ret ) ) {

        // Remember the original values
        left = style.left;
        rsLeft = elem.runtimeStyle && elem.runtimeStyle.left;

        // Put in the new values to get a computed value out
        if ( rsLeft ) {
          elem.runtimeStyle.left = elem.currentStyle.left;
        }
        style.left = name === "fontSize" ? "1em" : ( ret || 0 );
        ret = style.pixelLeft + "px";

        // Revert the changed values
        style.left = left;
        if ( rsLeft ) {
          elem.runtimeStyle.left = rsLeft;
        }
      }

      return ret === "" ? "auto" : ret;
    };

    return jQuery.noConflict();
  }
</script>

<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script src="https://github.com/mikesherov/jquery/raw/10f755b315ceb78399b316084cca390f7e78bcb4/src/css.js"></script>
<script>
  var matchReal1 = replace_rnumnopx(/^-?\d*(?:\d|\.\d+)(?!px)[^\d\s]+$/i);
</script>

<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script src="https://github.com/mikesherov/jquery/raw/10f755b315ceb78399b316084cca390f7e78bcb4/src/css.js"></script>
<script>
  var matchReal2 = replace_rnumnopx(/^-?(?:\d+|\d*\.\d+)(?!px)[^\d\s]+$/i);
</script>

<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script src="https://github.com/mikesherov/jquery/raw/10f755b315ceb78399b316084cca390f7e78bcb4/src/css.js"></script>
<script>
  var matchReal3 = replace_rnumnopx(/^-?(?:\d+(?:\.\d+)?|\.\d+)(?!px)[^\d\s]+$/i);
</script>

<div id="target"></div>

<script>
  var jQuery = matchInteger,
    $target = jQuery( "#target" ),
    property = "background-position",
    values = [ "12ex", "-12ex", "12ex center", "-12ex center" ],
    read_matchInteger = function(){ matchInteger( this ).css( property ); },
    read_matchReal1 = function(){ matchReal1( this ).css( property ); },
    read_matchReal2 = function(){ matchReal2( this ).css( property ); },
    read_matchReal3 = function(){ matchReal3( this ).css( property ); };

    jQuery.map( values, function( value ) {
      jQuery( "<div/>" ).css( property, value ).appendTo( $target );
    });
    var $elements = $target.children();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
/^-?\d+(?:px)?$/i
$elements.each( read_matchInteger );
ready
/^-?\d*(?:\d|\.\d+)(?!px)[^\d\s]+$/i
$elements.each( read_matchReal1 );
ready
(/^-?(?:\d+|\d*\.\d+)(?!px)[^\d\s]+$/i
$elements.each( read_matchReal2 );
ready
/^-?(?:\d+(?:\.\d+)?|\.\d+)(?!px)[^\d\s]+$/i
$elements.each( read_matchReal3 );
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.