selectortest

Benchmark created by selector testing 2 on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<ul id="test">
<li class="test">test</li>
<li class="test">test</li>
<li class="test">test</li>
<li class="test">test</li>
<li class="test">test</li>
<li class="test">test</li>
</ul>
<script>
  ;// just in case
  (function (win) {
    win.g = win.g || {init:[]};
  
    var doc = document;
    
    g.init.push(
      function _assignClassMethod() {
        if (doc.getElementsByClassName) {
          g.c = function _getElsByClass(className, context) {
            context = context || doc;
            return context.getElementsByClassName(className);
          };
        } else if (doc.evaluate) { // Supports XPath
          g.c = function _getElsByClass(className, context) {
            var tag = '*',
                context = context || doc,
                retArr = [];
  
            elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            while ((node = elements.iterateNext())) {
              retArr.push(node);
            }
  
            return retArr;
          };
        } else {
          g.c = function _getElsByClass(className, context) {
            context = context || doc; 
            var childElements = context.getElementsByTagName("*"),
                retArr = [],
                regex = new RegExp("(^|\\s)" + className + "(\\s|$)"); // can't use word boundaries (\b) because "help" and "help-me" would both be returned.
            for (var i = 0, len = childElements.length; i < len; i++) {
              if (regex.test(childElements[i].className))
                retArr.push(childElements[i]);
            }
  
            return retArr;
          };
        }
      },
  
      function _assignIdMethod() {
        g.i = function _getElementById(id) {
          return doc.getElementById(id);
        };
      },
  
      function _assignTagMethod() {
        g.t = function _getElementsByTag(tag, context) {
          context = context || doc;
          return context.getElementsByTagName(tag);
        };
      },
  
      function _assignFindMethod() {
        g.f = function _find(selector, context) {
          var iReg = /^#([-a-zA-Z0-9_]+)$/,
              cReg = /^\.([-a-zA-Z0-9_]+)$/,
              tReg = /^[a-zA-Z]+$/,
              context = context || doc,
              sizzle = jQuery && jQuery.find;
  
          if (iReg.test(selector))
            return g.i(selector.substring(1));
  
          if (cReg.test(selector))
            return g.c(selector.substring(1), context);
  
          if (tReg.test(selector))
            return g.t(selector, context);
  
          if (sizzle)
            return context ? sizzle(selector, context) : sizzle(selector);
  
          return false;
        };
      }
    );
  
    for (var i = 0, len = g.init.length; i < len; i++) {
      g.init[i]();
    }
  }(window));
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jquery
var test = $(".test");
ready
custom find
var test = g.f(".test");
ready
custom public
var test = g.c("test");
ready
custom public to jquery
var test = $(g.c("test"));
ready
custom find to jquery
var test = $(g.f(".test"));
ready

Revisions

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

  • Revision 1: published by selector testing 2 on
  • Revision 2: published on