#219 enhancement
Juriy Zaytsev

Improved Function#methodize

Reported by Juriy Zaytsev | July 12th, 2008 @ 07:25 AM

Continuing with Function.prototype.* optimizations, here's a "smarter" Function#methodize.

There are 34 Element methods in prototype.js that accept no arguments (more than a half of all) - Element.show, Element.hide, Element.visible, etc.

When extending DOM Elements, all these methods are "methodized" by one pattern - the one that accounts for extra arguments. Array.prototype.concat and $A are the usual party poopers when it comes to performance. By returning a "simpler" function when methodizing a parameter-less method, we can boost the speed of the entire library.

Function.prototype.methodize = function(noArgs) {
  if __method = this;
  return this._methodized = (noArgs === true)
    ? function() { return __method.call(null, this) }
    : function() { return __method.apply(null, [this].concat($A(arguments))) };
}

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins

Referenced by

Pages