Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does deep nesting flexbox layout cause performance issue?

Tags:

I have been working on a ReactJS project where I create most of the components using flexbox layout. Since with react, we can have deeply nested components, so my layout is having nested flexbox layout.

Now my question is, does this have any issue with performance? On a single page, there are many components and each component have 3 to 4 level nested flexbox layout. Will that cause a performance issue?

like image 933
Provash Shoumma Avatar asked Sep 13 '16 06:09

Provash Shoumma


People also ask

Does flexbox slow?

In general, it should be much faster in the common case, but you can construct a case where it's equally as slow. That said, if you can get away with it, regular block layout (non-float), will usually be as fast or faster than new flexbox since it's always single-pass.

What are the disadvantages of using flexbox?

Disadvantages. While flexbox is supported by most major browsers, it's still newer than the traditional box model. This means older browsers don't support it as well (some not at all). There are also more inconsistencies across different browsers.

When should I use flexbox layout?

Flexbox is a layout model that allows elements to align and distribute space within a container. Using flexible widths and heights, elements can be aligned to fill a space or distribute space between elements, which makes it a great tool to use for responsive design systems.

Is flexbox better than float?

These are the following reasons to use flexbox over floats.Flexbox is responsive and mobile-friendly. Flex container's margins do not collapse with the margins of its content. We can easily change the order of elements on our webpage without even making changes in HTML.


1 Answers

Have done a little test. Rendered 100 components, each with 10 nested layout. With and without flexbox. Here are the code snippets:

Component/index.js

@CSSModules(styles, { allowMultiple: true }) export default class TheComponent extends Component {   render() {     const { deepNest, flex } = this.props      return (       <div>{ this.renderComp(deepNest, flex) }</div>     )   }    renderComp(deepNest, flex) {     const flexProperties = [       { justifyContent: "center", alignItems: "center" },       { justifyContent: "flex-start", alignItems: "flex-end" },       { flexDirection: "row" }     ]      const content = [       "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus interdum quis ligula vel elementum. Integer non rhoncus purus, eget dignissim ante.",       "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",       "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus interdum quis ligula vel elementum. Integer non rhoncus purus, eget dignissim ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus interdum quis ligula vel elementum. Integer non rhoncus purus, eget dignissim ante."     ]      if (deepNest > 0 && flex) {       return (         <div styleName="containerFlex" style={flexProperties[deepNest % 3]}>           <div styleName="contentFlex" style={flexProperties[deepNest % 3]}>             { content[deepNest % 3] }           </div>           <div styleName="nestedFlex" style={flexProperties[deepNest % 3]}>             { this.renderComp(deepNest - 1, flex) }           </div>         </div>       )     }      if (deepNest > 0 && !flex) {       return (         <div styleName="container">           <div styleName="content">             Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus interdum quis ligula vel elementum. Integer non rhoncus purus, eget dignissim ante.           </div>           <div styleName="nested">             { this.renderComp(deepNest - 1, flex) }           </div>         </div>       )     }   } } 

WithFlex/index.js

import TheComponent from "../Component"  @CSSModules(styles, { allowMultiple: true }) export default class WithFlex extends Component {   constructor(props) {     super(props)      this.state = { render: false }   }    render() {     const {render} = this.state      // number of components to render     const arr = _.range(100)      return (       <div>         <div         style={{ display: "block", padding: 30, lineHeight: "60px" }}         onClick={() => this.setState({render: !render})}>             Start Render         </div>          { render && arr.map((i) => <TheComponent key={i} deepNest={10} flex={true}/> ) }       </div>     )   } } 

WithoutFlex/index.js

import TheComponent from "../Component"  @CSSModules(styles, { allowMultiple: true }) export default class WithoutFlex extends Component {   constructor(props) {     super(props)      this.state = { render: false }   }    render() {     const {render} = this.state      // number of components to renders     const arr = _.range(100)      return (       <div>         <div         style={{ display: "block", padding: 30, lineHeight: "60px" }}         onClick={() => this.setState({render: !render})}>             Start Render         </div>          { render && arr.map((i) => <TheComponent key={i} deepNest={10} flex={false}/> ) }       </div>     )   } } 

Results from Chrome dev-tool timeline.

WithFlex

enter image description here

WithoutFlex

enter image description here

Summary

The difference is not that much. Also in flexbox, I put random properties to choose from. So I think it's alright with the performance. Hope it will help other devs.

like image 93
Provash Shoumma Avatar answered Sep 19 '22 15:09

Provash Shoumma