jQuery.fly() - flyweight pattern (v3)

Revision 3 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($) {
  
   var fly = $(),
       push = Array.prototype.push;
  
   $.fly = function(elem) {
    var len = fly.length,
        i;
    if ($.isArray(elem)) {
     fly.length = 0;
     i = push.apply(fly, elem);
    } else {
     if (elem instanceof $) {
      return elem;
     }
     if (typeof elem == "string") {
      throw "use jQuery()";
     }
     fly[0] = elem;
     fly.length = i = 1;
    }
    // remove orphaned references
    while (i < len) {
     delete fly[i++];
    }
  
    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