jQuery.text() (v2)

Revision 2 of this benchmark created by Anton M. on


Description

Only test the .text/.textFast methods not the jQuery selector engine

Preparation HTML

<script src="https://code.jquery.com/jquery.js"></script>

<div>
   <h3>cool.</h3>
   <!-- comment -->
   text node
   <div>
      <div>inception</div>
   </div>
</div>
<script>
  var div = jQuery("div:first");
  jQuery.fn.textFast = function(text) {
   if (jQuery.isFunction(text)) {
    return this.each(function(i) {
     var self = jQuery(this);
     self.text(text.call(this, i, self.text()));
    });
   }
   if (typeof text !== "object" && text !== undefined) {
    return this.empty().append((this[0] && this[0].ownerDocument || document).createTextNode(text));
   }
   return jQuery.textFast(this);
  }
  
  jQuery.textFast = function(elems) {
   var ret = "",
       elem;
  
   for (var i = 0; elems[i]; i++) {
    elem = elems[i];
    // thie part is new.
    ret += elem.textContent || elem.innerText || Sizzle.getText(elem.childNodes);
   }
  
   return ret;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Regular jQuery.text()
div.text();
ready
Optimized text()
div.textFast();
ready

Revisions

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

  • Revision 2: published by Anton M. on