jQuery.fly() - flyweight pattern (v2)

Revision 2 of this benchmark created by Matjaž Lipuš on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var a = $('<div/>').append(Array(100).join('<a></a>')).find('a');
  
  (function($, undef) {
  
   var fly = $([]),
       ctx = fly.context,
       push = Array.prototype.push;
  
   $.fly = function(elem) {
    elem ||   (elem = ctx);
    if (elem.nodeType || elem.length === undef) {
     fly[0] = fly.context = elem;
     fly.length = 1;
    } else {
     if (elem instanceof $) {
      return elem;
     }
     if (typeof elem == "string") {
      throw "use jQuery()";
     }
     fly.context = ctx;
     fly.length = 0;
     push.apply(fly, elem);
    }
    return fly;
   };
  
  })(jQuery);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Iterate using jQuery object
a.each(function() {
 $(this);
});
ready
Iterate using jQuery.fly()
a.each(function() {
 $.fly(this);
});
ready

Revisions

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

  • Revision 2: published by Matjaž Lipuš on
  • Revision 3: published by Matjaž Lipuš on
  • Revision 4: published by Matjaž Lipuš on