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 with 13 additions and 11 deletions.
  1. +13 −11 src/css.js
@@ -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

This comment has been minimized.

Copy link
Contributor

dcherman replied Nov 11, 2011

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

This comment has been minimized.

Copy link
Member Author

mikesherov replied Nov 11, 2011

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

@rwaldron

This comment has been minimized.

Copy link
Member

rwaldron replied Nov 11, 2011

For 1 character? All we care about is:

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

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.