Hi,
I had some time and used it to read through the current SVN-code searching for
possible improvements. My time was not enough to go through all the code, but
at least I could check jquery.js. I have some suggestions which you might
whant to evaluate.
I. each
-------
This implementation is a bit longer but should usually be faster as well:
----
each: Array.forEach?function( obj, fn, args ) {
var fe = (obj.length == undefined)?
function(o,f) {
for( var i in o ) f.call(o[i],i);
}:Array.forEach;
if( args ) fe(obj,function(o){ fn.apply(o,args); });
else fe(obj,function(o,i){ fn.apply(o,[i,o]); });
return obj;
}:function( obj, fn, args ) {
var fe = (obj.length == undefined)?
function(o,f) {
for( var i in o ) f.call(o[i],i);
}:function(o,f) {
for( var i = 0; i < o.length; i++ ) f.call(o[i],i);
};
if( args ) fe(obj,function(o){ fn.apply(o,args); });
else fe(obj,function(o,i){ fn.apply(o,[i,o]); });
return obj;
},
----
1. In case a native implementation is available I expect it to be faster than
any other possible solution (use Array.forEach if avaliable).
2. I took the check for the parameter 'args' out of the loop. It only has to
be evaluated once.
II. className
-------------
className: {
add: function(o,c){
if (jQuery.className.has(o,c)) return;
o.className += ( o.className ? " " : "" ) + c;
},
remove: function(o,c){
if( !c ) { o.className = ''; return; }
var cns = o.className.split(' ');
var cns2 = [];
for( var i = 0; i < cns.length; i++ ) if( cns[i] != c )
cns2.push(cns[i]);
o.className = cns2.join(' ');
},
has: function(e,a) {
if ( e.className != undefined ) e = e.className;
var cns = e.split(' ');
for( var i = 0; i < cns.length; i++ ) if( cns[i] != a ) return
true;
return false;
}
},
I'm not shure if that really speeds up things so much. Generally speaking
regular expressions need more calculations to be evaluated than a simple
character comparisions.
I guess, many people use these className-manipulation functions quite often.
III new function() {...}
------------------------
I am pretty shure that (function() {...})() is faster. You spare the creation
of an object that you never use for each of these constructs.
On the other hand I have not found these constructs in some inner loops yet.
The overhead might be acceptable here, but I also don't think that new
function() {...} is less readable than (function() {...})().
I have not measured these suggestions now. What do you think about them?
Christof
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/