On 29 avr, 04:34, Dave Methvin <[EMAIL PROTECTED]> wrote: > > I have this piece of code that works fine, but is a bit slow (on a > > page with =~ 25 container and =~ 70 buttons, it takes about 2 to 3 > > seconds to complete, even after I made my best to narrow down the $ > > (elt) set on which it is called). So I if anybody here has any idea to > > improve it... > > Do you have a page we can look at?
Not yet, but there may be one later today. > Is it the setup, or is the 2-3 > seconds when you click on something? Sorry, forgot to mention this. And yes, it's the setup that's taking too long. > Firebug's profiler can tell you a > lot about where it's spending the time. Mainly in attr() and curCSS. Rereading my code (and double-checking the markup), I stripped the code down to this: $.fn.OneOfTogglers = function(options) { options = $.extend({}, $.fn.OneOfTogglers.defaults, options); var containerSelector = options.togglerContainerSelector; var buttonClass = options.buttonClass; var buttonSelector = options.buttonElt + "." + buttonClass; var targetClass = options.targetClass; var targetSelector = options.targetElt + "." + targetClass; var buttonOpen = options.buttonOpen; var buttonClose = options.buttonClose; var setClosedState = function() { $("img", this).attr(buttonOpen); }; $(containerSelector, this).each(function() { var domchunk = this; var $targets = $(targetSelector, domchunk).hide(); var $buttons = $(buttonSelector, domchunk); $buttons.click(function() { var targetId = "#" + this.id.replace(buttonClass, targetClass); var $target = $(targetId, domchunk) var $img = $('img', this); var closed = $target.css('display') == 'none'; // are we closed yet ? if (closed) { // if so, close the other $targets.not(targetId).hide(); $buttons.each(setClosedState); // then open this $img.attr(buttonClose); $target.show(); } else { $img.attr(buttonOpen); $target.hide(); } return false; }); }); return this; }; Now most of the time is spent into curCSS() and data()... <slightly ot> Now there's somethings that bugs me (no pun intented) wrt/ firebug's profiler - the report starts with: $blockContent.OneOfTogglers (*177.877ms*, 2940 calls) (which btw looks like an improvement wrt/ the 577.xxx ms from the previous version), *but* later in the details there is: OneOfTogglers 1 0.04% 0.072ms *1960.658ms* Needless to say, this last line (the cumulated time column) is way closer to what I get from the Q&D timestamping before and after the call. </slightly ot>