Skip to content

[Feature Request] Can we do some Static Analysis for diff with babel ? #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
coppyC opened this issue Oct 8, 2020 · 6 comments
Closed

Comments

@coppyC
Copy link

coppyC commented Oct 8, 2020

react don't konw which prop is static,but can we get the help with babel ?

like this.

// jsx
<div className="xx" style={{height: 8}} onClick={click}>{children}</div>

// js after babel
React.createElement("div", {
  className: "xx",
  style: {height: 8} ,
  onClick: click,
  __reactDiff__: ['onClick', 'children']   // It's different in this line. 
}, children)

then, react will know, the props need to diff are only onClick and children.

Is it feasible?

@theKashey
Copy link

@coppyC
Copy link
Author

coppyC commented Oct 8, 2020

It's different.

what I say is accelerate react diff.

function Hello(props) {
  <div className="xx" style={{height: 8}}>
    {props.children}
  </div>
}

As I know, in this example, when update the component, react in order to do the diff algorithm, will compare the className, style and children

but the className and style don't use any variables, they don't need to diff.

And react can't judge "Are className is constant value?", then react will compare className

so I think can Babel tell react, what need to compare.

@theKashey
Copy link

an extra operation to check what shall be compared might take more time than the actual compare.

@coppyC
Copy link
Author

coppyC commented Oct 9, 2020

I can't understand why it is an extra operation.
I think it's an other operation to replace. like this.

// (exclude Spread Operator like `<div {...props}></div>`)

Object.keys(props).forEach(key => {
  // compare
})

// `dynamicProps` is an string array that babel generate
dynamicProps.forEach(keys => {
  // compare
})

I think dynamicProps.length <= Object.keys(props).length

Actually, many components are constant, they event don't need to compare.

@NE-SmallTown
Copy link

NE-SmallTown commented Oct 9, 2020

As I know, in this example, when update the component, react in order to do the diff algorithm, will compare the className, style and children

No, React will just use === to compare the props object, not iterate it

But back to your concerned topic, from my personal perspective, the static/runtime compiler optimization will not be the key goal of React. It has tried prepack and find that is not the way to go, It will provide/figure out a different way to do the 'optimization' based on the UI(research) and (runtime)Js and UX/DX just like they did before(JIT, heuristic, mount/update hooks, JND, GC, something for V8, ...)

@coppyC
Copy link
Author

coppyC commented Oct 9, 2020

ok, I understand . Thanks.

@coppyC coppyC closed this as completed Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants