getWH()
getWH()
getWH() is often the highest jQuery function in my profile.
function getWH() {
val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
var padding = 0, border = 0;
jQuery.each( which, function() {
padding += parseFloat(jQuery.curCSS( elem, "padding" + this, true))
|| 0;
border += parseFloat(jQuery.curCSS( elem, "border" + this + "Width",
true)) || 0;
});
val -= Math.round(padding + border);
}
I traced through and there's a lot going on in curCSS(), but
surprisingly a lot of the time is spent in getWH().
I notice that you don't really need both the "padding" and "border"
variables--just a single "sum" variable would do. That doesn't help
speed much, but it might save a few bytes, and I don't think the code
would be any harder to read or maintain.
function getWH() {
val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
var sum = 0;
jQuery.each( which, function() {
sum += +(jQuery.curCSS( elem, "padding" + this, true)) || 0;
sum += +(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
});
val -= Math.round(sum);
}
I used the unary plus, which is supposedly faster (and obviously
shorter) than parseFloat.
The || 0 guards against NaN being passed through. I don't see a way to
avoid those. I wonder about the Math.round(). I put a check in to see
if (sum==Math.round(sum)), and it always did, so it does nothing in my
code. But it doesn't seem like it would be there unless someone had
been getting a not-quite-integer at some point.
It'd be great to ditch the Math.round() if it's only in there for
historical reasons,but I know I've gotten fractional values before
when I expected an integer.