IssueRaiser

Optimizing Compiler: Component Folding

This is like the final frontier for React but I never really wrote anything about it so I figured I'd create an issue. Basically, the idea is to utilize information about how React works to do [constant folding](https://en.wikipedia.org/wiki/Constant_folding) and [inlining](https://en.wikipedia.org/wiki/Inline_expansion) of components under certain conditions. Example Source: ``` js function Foo(props) { if (props.data.type === 'img') { return <img src={props.data.src} className={props.className} alt={props.alt} />; } return <span>{props.data.type}</span>; } Foo.defaultProps = { alt: "An image of Foo." }; ``` ``` js var CSSClasses = { bar: 'bar' }; module.exports = CSSClasses; ``` ``` js var Foo = require('Foo'); var Classes = require('Classes'); function Bar(props) { return <Foo data={{ type: 'img', src: props.src }} className={Classes.bar} />; } ``` By knowing what Foo and Classes is made up of, we can turn the Bar component into this: ``` js var Foo = require('Foo'); var Classes = require('Classes'); function Bar(props) { return <Foo data={{ type: 'img', src: props.src }} className={Classes.bar} />; } function Bar_optimized(props) { return <img src={props.src} className="Bar" alt="An image of Foo." />; } ``` [Dead-code elimination](https://en.wikipedia.org/wiki/Dead_code_elimination) then strips it down to just: ``` js function Bar_optimized(props) { return <img src={props.src} className="Bar" alt="An image of Foo." />; } ``` Now there are a

Posted by sebmarkbage on Jul 21, 2016, 12:21 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.5,000 issues0 comments

reliability (Software)

Optimizing Compiler: Component Folding

sebmarkbageJul 21, 2016, 12:21 AM

This is like the final frontier for React but I never really wrote anything about it so I figured I'd create an issue. Basically, the idea is to utilize information about how React works to do [constant folding](https://en.wikipedia.org/wiki/Constant_folding) and [inlining](https://en.wikipedia.org/wiki/Inline_expansion) of components under certain conditions. Example Source: ``` js function Foo(props) { if (props.data.type === 'img') { return <img src={props.data.src} className={props.className} alt={props.alt} />; } return <span>{props.data.type}</span>; } Foo.defaultProps = { alt: "An image of Foo." }; ``` ``` js var CSSClasses = { bar: 'bar' }; module.exports = CSSClasses; ``` ``` js var Foo = require('Foo'); var Classes = require('Classes'); function Bar(props) { return <Foo data={{ type: 'img', src: props.src }} className={Classes.bar} />; } ``` By knowing what Foo and Classes is made up of, we can turn the Bar component into this: ``` js var Foo = require('Foo'); var Classes = require('Classes'); function Bar(props) { return <Foo data={{ type: 'img', src: props.src }} className={Classes.bar} />; } function Bar_optimized(props) { return <img src={props.src} className="Bar" alt="An image of Foo." />; } ``` [Dead-code elimination](https://en.wikipedia.org/wiki/Dead_code_elimination) then strips it down to just: ``` js function Bar_optimized(props) { return <img src={props.src} className="Bar" alt="An image of Foo." />; } ``` Now there are a

Comments (0)

No comments yet. Start the discussion.

Comment on this issue