[jQuery] $() speedup available for testing
I've posted a patched copy of jQuery that speeds up the $() function by
using $.fn as a prototype for the object that $() returns, instead of
copying all of the $.fn methods each time $() is called. When you use
plugins that create many $.fn methods, such as the event plugin, that copy
loop takes a lot of time.
The new code eliminates that fixed per-call overhead. This speeds up every
$() call, especially simple $(element) wrappers which are now very fast--at
least if you're not combining jQuery with Prototype.js. Unfortunately, if
Prototype.js is loaded, there is compatibility code that runs on every $()
call and loops through all the $.fn functions. I didn't change that code at
all. Maybe it would be possible to make some similar improvement to the
Prototype.js compatibility code; I don't use Prototype so I didn't look at
that.
If anyone would like to try this out and report any problems, I'd greatly
appreciate it. I've tested it without Prototype.js, but if anyone has an app
that uses both jQuery and Prototype.js and doesn't mind trying it, I would
especially like to hear if it works or not. If the code doesn't break
anything, it will probably show up in the official jQuery soon.
The change should hopefully be transparent to existing code, except for two
changes that could affect plugins.
First, because $.fn is now the prototype for all $() objects, any changes to
it affect all existing as well as future $() objects. If you add a new
$.fn.foobar() function, every $() object immediately gains a foobar()
method. Before, changes to $.fn affected only future $() calls, not $()
return value objects that you have already saved references to. It seems
unlikely that any code would depend on this old behavior.
Also, the new code removes a small misfeature: Previously, if a plugin
provided a $.fn method with the same name as an existing method, the
previous method was renamed with a leading underscore. For example, if you
define a function $.fn.html(), it becomes available as $().html(), and the
existing $().html() function becomes $()._html(). This does not work with
the new code. If you want to replace an existing $() method and need to be
able to call the old one, simply save a reference to the old method before
replacing it. (The underscore trick breaks anyway if two plugins both try to
wrap the same method, which is why I called it a misfeature.)
The individual jquery.js file (with no plugins) is here:
http://geary.jquery.com/jquery/jquery/jquery.js
And the merged file (with fx, event, and ajax plugins) is here:
http://geary.jquery.com/jquery/jquery-all.js
The original files these are based on (current SVN versions) are here for
comparison. If you do run into any problems with the new code, please try
the corresponding file below to see if the problem is caused by my change or
already existed in SVN:
http://geary.jquery.com/jquery/jquery/jquery-old.js
http://geary.jquery.com/jquery/jquery-all-old.js
Thanks!
-Mike
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/