Skip to content
Permalink
Browse files
use for loop instead of jQuery.each in getWH()
  • Loading branch information
mikesherov authored and dmethvin committed Nov 11, 2011
1 parent 774eba3 commit e0a4aa415d988b21c5b666a20ab00823673ffc3d
Showing 1 changed file with 13 additions and 11 deletions.
@@ -327,20 +327,22 @@ function getWH( elem, name, extra ) {

// Start with offset property
var val = name === "width" ? elem.offsetWidth : elem.offsetHeight,
which = name === "width" ? cssWidth : cssHeight;
which = name === "width" ? cssWidth : cssHeight,
i = 0,
len = which.length;

if ( val > 0 ) {
if ( extra !== "border" ) {
jQuery.each( which, function() {
for ( ; i < len; i++ ) {
if ( !extra ) {
val -= parseFloat( jQuery.css( elem, "padding" + this ) ) || 0;
val -= parseFloat( jQuery.css( elem, "padding" + which[ i ] ) ) || 0;
}
if ( extra === "margin" ) {
val += parseFloat( jQuery.css( elem, extra + this ) ) || 0;
val += parseFloat( jQuery.css( elem, extra + which[ i ] ) ) || 0;
} else {
val -= parseFloat( jQuery.css( elem, "border" + this + "Width" ) ) || 0;
val -= parseFloat( jQuery.css( elem, "border" + which[ i ] + "Width" ) ) || 0;
}
});
}
}

return val + "px";
@@ -356,15 +358,15 @@ function getWH( elem, name, extra ) {

// Add padding, border, margin
if ( extra ) {
jQuery.each( which, function() {
val += parseFloat( jQuery.css( elem, "padding" + this ) ) || 0;
for ( ; i < len; i++ ) {
val += parseFloat( jQuery.css( elem, "padding" + which[ i ] ) ) || 0;
if ( extra !== "padding" ) {
val += parseFloat( jQuery.css( elem, "border" + this + "Width" ) ) || 0;
val += parseFloat( jQuery.css( elem, "border" + which[ i ] + "Width" ) ) || 0;
}
if ( extra === "margin" ) {
val += parseFloat( jQuery.css( elem, extra + this ) ) || 0;
val += parseFloat( jQuery.css( elem, extra + which[ i ] ) ) || 0;
}
});
}
}

return val + "px";

3 comments on commit e0a4aa4

@dcherman
Copy link
Contributor

@dcherman dcherman commented on e0a4aa4 Nov 11, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know how much you care, but since order of execution shouldn't matter here, a reverse while loop would save you

249430 (-29) jquery.js
94160 (-10) jquery.min.js
33319 (-1) jquery.min.js.gz

@mikesherov
Copy link
Member Author

@mikesherov mikesherov commented on e0a4aa4 Nov 11, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's actually a good point. also, one less var to declare.

@rwaldron
Copy link
Member

@rwaldron rwaldron commented on e0a4aa4 Nov 11, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 1 character? All we care about is:

33319 (-1) jquery.min.js.gz

Please sign in to comment.