@@ -560,9 +560,9 @@ d3.layout.partition = function() {
560560 node . y = node . depth * dy ;
561561 node . dx = dx ;
562562 node . dy = dy ;
563- if ( children ) {
563+ if ( children && ( n = children . length ) ) {
564564 var i = - 1 ,
565- n = children . length ,
565+ n ,
566566 c ,
567567 d ;
568568 dx = node . value ? dx / node . value : 0 ;
@@ -576,9 +576,9 @@ d3.layout.partition = function() {
576576 function depth ( node ) {
577577 var children = node . children ,
578578 d = 0 ;
579- if ( children ) {
579+ if ( children && ( n = children . length ) ) {
580580 var i = - 1 ,
581- n = children . length ;
581+ n ;
582582 while ( ++ i < n ) d = Math . max ( d , depth ( children [ i ] ) ) ;
583583 }
584584 return 1 + d ;
@@ -1477,9 +1477,9 @@ d3.layout.tree = function() {
14771477 function secondWalk ( node , x ) {
14781478 node . x = node . _tree . prelim + x ;
14791479 var children = node . children ;
1480- if ( children ) {
1480+ if ( children && ( n = children . length ) ) {
14811481 var i = - 1 ,
1482- n = children . length ;
1482+ n ;
14831483 x += node . _tree . mod ;
14841484 while ( ++ i < n ) {
14851485 secondWalk ( children [ i ] , x ) ;
@@ -1584,18 +1584,21 @@ function d3_layout_treeSeparation(a, b) {
15841584// }
15851585
15861586function d3_layout_treeLeft ( node ) {
1587- return node . children ? node . children [ 0 ] : node . _tree . thread ;
1587+ var children = node . children ;
1588+ return children && children . length ? children [ 0 ] : node . _tree . thread ;
15881589}
15891590
15901591function d3_layout_treeRight ( node ) {
1591- return node . children ? node . children [ node . children . length - 1 ] : node . _tree . thread ;
1592+ var children = node . children ,
1593+ n ;
1594+ return children && ( n = children . length ) ? children [ n - 1 ] : node . _tree . thread ;
15921595}
15931596
15941597function d3_layout_treeSearch ( node , compare ) {
15951598 var children = node . children ;
1596- if ( children ) {
1599+ if ( children && ( n = children . length ) ) {
15971600 var child ,
1598- n = children . length ,
1601+ n ,
15991602 i = - 1 ;
16001603 while ( ++ i < n ) {
16011604 if ( compare ( child = d3_layout_treeSearch ( children [ i ] , compare ) , node ) > 0 ) {
@@ -1621,11 +1624,11 @@ function d3_layout_treeDeepest(a, b) {
16211624function d3_layout_treeVisitAfter ( node , callback ) {
16221625 function visit ( node , previousSibling ) {
16231626 var children = node . children ;
1624- if ( children ) {
1627+ if ( children && ( n = children . length ) ) {
16251628 var child ,
16261629 previousChild = null ,
16271630 i = - 1 ,
1628- n = children . length ;
1631+ n ;
16291632 while ( ++ i < n ) {
16301633 child = children [ i ] ;
16311634 visit ( child , previousChild ) ;
@@ -1693,57 +1696,61 @@ d3.layout.treemap = function() {
16931696
16941697 // Recursively arranges the specified node's children into squarified rows.
16951698 function squarify ( node ) {
1696- if ( ! node . children ) return ;
1697- var rect = pad ( node ) ,
1698- row = [ ] ,
1699- children = node . children . slice ( ) , // copy-on-write
1700- child ,
1701- best = Infinity , // the best row score so far
1702- score , // the current row score
1703- u = Math . min ( rect . dx , rect . dy ) , // initial orientation
1704- n ;
1705- scale ( children , rect . dx * rect . dy / node . value ) ;
1706- row . area = 0 ;
1707- while ( ( n = children . length ) > 0 ) {
1708- row . push ( child = children [ n - 1 ] ) ;
1709- row . area += child . area ;
1710- if ( ( score = worst ( row , u ) ) <= best ) { // continue with this orientation
1711- children . pop ( ) ;
1712- best = score ;
1713- } else { // abort, and try a different orientation
1714- row . area -= row . pop ( ) . area ;
1715- position ( row , u , rect , false ) ;
1716- u = Math . min ( rect . dx , rect . dy ) ;
1699+ var children = node . children ;
1700+ if ( children && children . length ) {
1701+ var rect = pad ( node ) ,
1702+ row = [ ] ,
1703+ remaining = children . slice ( ) , // copy-on-write
1704+ child ,
1705+ best = Infinity , // the best row score so far
1706+ score , // the current row score
1707+ u = Math . min ( rect . dx , rect . dy ) , // initial orientation
1708+ n ;
1709+ scale ( remaining , rect . dx * rect . dy / node . value ) ;
1710+ row . area = 0 ;
1711+ while ( ( n = remaining . length ) > 0 ) {
1712+ row . push ( child = remaining [ n - 1 ] ) ;
1713+ row . area += child . area ;
1714+ if ( ( score = worst ( row , u ) ) <= best ) { // continue with this orientation
1715+ remaining . pop ( ) ;
1716+ best = score ;
1717+ } else { // abort, and try a different orientation
1718+ row . area -= row . pop ( ) . area ;
1719+ position ( row , u , rect , false ) ;
1720+ u = Math . min ( rect . dx , rect . dy ) ;
1721+ row . length = row . area = 0 ;
1722+ best = Infinity ;
1723+ }
1724+ }
1725+ if ( row . length ) {
1726+ position ( row , u , rect , true ) ;
17171727 row . length = row . area = 0 ;
1718- best = Infinity ;
17191728 }
1729+ children . forEach ( squarify ) ;
17201730 }
1721- if ( row . length ) {
1722- position ( row , u , rect , true ) ;
1723- row . length = row . area = 0 ;
1724- }
1725- node . children . forEach ( squarify ) ;
17261731 }
17271732
17281733 // Recursively resizes the specified node's children into existing rows.
17291734 // Preserves the existing layout!
17301735 function stickify ( node ) {
1731- if ( ! node . children ) return ;
1732- var rect = pad ( node ) ,
1733- children = node . children . slice ( ) , // copy-on-write
1734- child ,
1735- row = [ ] ;
1736- scale ( children , rect . dx * rect . dy / node . value ) ;
1737- row . area = 0 ;
1738- while ( child = children . pop ( ) ) {
1739- row . push ( child ) ;
1740- row . area += child . area ;
1741- if ( child . z != null ) {
1742- position ( row , child . z ? rect . dx : rect . dy , rect , ! children . length ) ;
1743- row . length = row . area = 0 ;
1736+ var children = node . children ;
1737+ if ( children && children . length ) {
1738+ var rect = pad ( node ) ,
1739+ remaining = children . slice ( ) , // copy-on-write
1740+ child ,
1741+ row = [ ] ;
1742+ scale ( remaining , rect . dx * rect . dy / node . value ) ;
1743+ row . area = 0 ;
1744+ while ( child = remaining . pop ( ) ) {
1745+ row . push ( child ) ;
1746+ row . area += child . area ;
1747+ if ( child . z != null ) {
1748+ position ( row , child . z ? rect . dx : rect . dy , rect , ! remaining . length ) ;
1749+ row . length = row . area = 0 ;
1750+ }
17441751 }
1752+ children . forEach ( stickify ) ;
17451753 }
1746- node . children . forEach ( stickify ) ;
17471754 }
17481755
17491756 // Computes the score for the specified row, as the worst aspect ratio.
0 commit comments