By using the Firebug profiler I realized that the "merge()" function is one of the most often called function during element selection or matching and is the second most consuming function in a slickspeed run (7880 calls 27.6% time jquery 1.2.3.js, linux FF 2.0.12).
These are the reasons for which I thought this method may be improved:
- IE is checked for each call, even if we are using other browsers
- the loop is looking up the same array twice instead of just once
- there is a written (only) duplication of the for loop
Take the last less seriously...but only one executes and cross browser debugging requires two hand written break-points...ha.ha.ha...
Probably this has been exacerbated by my last works ;-)
However even if it looks ugly what it does is simple, a function for each browser is compiled runtime when the absolute first call hit the function, and this is also the most compact way of writing it, well after removing the unnecessary concatenation fat.
merge: function( first, second ) {
// We have to loop this way because IE & Opera overwrite the length
// expando of getElementsByTagName
// Also, we need to make sure that the correct elements are being returned
// (IE returns comment nodes in a '*' query)
this.merge = new Function('first, second',
'var a, i = -1;'+
'while (a = second[++i])'+
(jQuery.browser.msie ? 'if ( a.nodeType != 8 )' : '')+
'first[first.length] = a;'+
'return first;'
);
return this.merge( first, second );
},
on the thread we can probably look at less eye intrusive version.
Please tell if I should build/attach a real diff once this has been reviewed.
One catch is that after this, merge() shows up as an anonymous function, don't know if jQuery use some trick on fn names,
however it worked and now slickspeed numbers are different and looking at the profiler again have...7880 calls 17.8% time.
Well surely need review...gain start to be suspiciously high...
did I miss the obvious ?
Diego Perini