Jquery 1.7.2 quickIs vs Jquery 1.9.1 .find()

JavaScript performance comparison

Revision 4 of this test case created by

Preparation code

<fieldset class="someclass">
  <legend>match</legend>
  <table>
    <tbody>
      <tr>
        <td>
          <table>
            <tbody>
              <tr>
                <td>
                  <span>
                    <a href="#">
                      <code>
                        <b id="target">target</b>
                      </code>
                    </a>
                  </span>
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
    </tbody>
  </table>
</fieldset>
<script src = "http://code.jquery.com/jquery-1.9.1.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var quickMatch, matches, handlerQueue, cur,
    rquickIs = /^(\w*)(?:#([\w\-]+))?(?:\.([\w\-]+))?$/,
    quickParse = function( selector ) {
      var quick = rquickIs.exec( selector );
      if ( quick ) {
        //   0  1    2   3
        // [ _, tag, id, class ]
        quick[1] = ( quick[1] || "" ).toLowerCase();
        quick[3] = quick[3] && new RegExp( "(?:^|\\s)" + quick[3] + "(?:\\s|$)" );
      }
      return quick;
    },
    quickIs = function( elem, m ) {
      var attrs = elem.attributes || {};
      return (
        (!m[1] || elem.nodeName.toLowerCase() === m[1]) &&
        (!m[2] || (attrs.id || {}).value === m[2]) &&
        (!m[3] || m[3].test( (attrs[ "class" ] || {}).value ))
      );
    },
    handleObj = {},
    root = document.body,
    sel = ".someclass",
    target = $("#target")[0];
  quickMatch = quickParse( sel );

};
</script>

Preparation code output

<fieldset class="someclass"> <legend>match</legend> <table> <tbody> <tr> <td> <table> <tbody> <tr> <td> <span> <a href="#"> <code> <b id="target">target</b> </code> </a> </span> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </fieldset>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
1.7.2
handlerQueue = [];
cur = target;

for ( ; cur != root; cur = cur.parentNode || root ) {
	/* jshint eqeqeq: true */

	// Don't check non-elements (#13208)
	// Don't process clicks on disabled elements (#6911, #8165, #11382, #11764)
	//simplified if ( cur.nodeType === 1 && (cur.disabled !== true || event.type !== "click") ) {
	if ( cur.nodeType === 1 && cur.disabled !== true ) {
		matches = [];
		//simplified for ( i = 0; i < delegateCount; i++ ) {
			//simplified handleObj = handlers[ i ];

			// Don't conflict with Object.prototype properties (#13203)
			//simplified sel = handleObj.selector + " ";

			//simplified if ( matches[ sel ] === undefined ) {
//------------------------------------------------------
        matches[ sel ] = quickIs( cur, quickMatch );
//------------------------------------------------------
			//simplified }
			if ( matches[ sel ] ) {
				matches.push( handleObj );
			}
		//simplified }
		if ( matches.length ) {
			handlerQueue.push({ elem: cur, handlers: matches });
		}
	}
}
pending…
1.9.1
handlerQueue = [];
cur = target;

for ( ; cur != root; cur = cur.parentNode || root ) {
	/* jshint eqeqeq: true */

	// Don't check non-elements (#13208)
	// Don't process clicks on disabled elements (#6911, #8165, #11382, #11764)
	//simplified if ( cur.nodeType === 1 && (cur.disabled !== true || event.type !== "click") ) {
	if ( cur.nodeType === 1 && cur.disabled !== true ) {
		matches = [];
		//simplified for ( i = 0; i < delegateCount; i++ ) {
			//simplified handleObj = handlers[ i ];

			// Don't conflict with Object.prototype properties (#13203)
			//simplified sel = handleObj.selector + " ";

			//simplified if ( matches[ sel ] === undefined ) {
				matches[ sel ] = //simplified handleObj.needsContext ?
					//simplified jQuery( sel, this ).index( cur ) >= 0 :
//------------------------------------------------------------
					jQuery.find( sel, root, null, [ cur ] ).length;
//------------------------------------------------------------
			//simplified }
			if ( matches[ sel ] ) {
				matches.push( handleObj );
			}
		//simplified }
		if ( matches.length ) {
			handlerQueue.push({ elem: cur, handlers: matches });
		}
	}
}
pending…
potential speedup
handlerQueue = [];
cur = target;

for ( ; cur != root; cur = cur.parentNode || root ) {
	/* jshint eqeqeq: true */

	// Don't check non-elements (#13208)
	// Don't process clicks on disabled elements (#6911, #8165, #11382, #11764)
	//simplified if ( cur.nodeType === 1 && (cur.disabled !== true || event.type !== "click") ) {
	if ( cur.nodeType === 1 && cur.disabled !== true ) {
		matches = [];
		//simplified for ( i = 0; i < delegateCount; i++ ) {
			//simplified handleObj = handlers[ i ];

			// Don't conflict with Object.prototype properties (#13203)
			//simplified sel = handleObj.selector + " ";

			//simplified if ( matches[ sel ] === undefined ) {
				matches[ sel ] = //simplified handleObj.needsContext ?
					//simplified jQuery( sel, this ).index( cur ) >= 0 :
//------------------------------------------------------------
					jQuery.find.matchesSelector( cur, sel );
//------------------------------------------------------------
			//simplified }
			if ( matches[ sel ] ) {
				matches.push( handleObj );
			}
		//simplified }
		if ( matches.length ) {
			handlerQueue.push({ elem: cur, handlers: matches });
		}
	}
}
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.

0 Comments

match
target