react icon indicating copy to clipboard operation
react copied to clipboard

How to record Perf mesures on every single render?

Open slorber opened this issue 11 years ago • 15 comments

I have an app with a framework very similar to Om where I always render from the top component. There is no Flux store with components listening to a single Flux store event, just a single global json state managed outside of React.

I ask this question as a follow-up to this issue, as I just noticed that React.render takes a callback: https://github.com/facebook/react/issues/1931

My former Perf code looked like this:

// 1st render
React.addons.Perf.start();
React.render(<App state=globalState/>, document.body);
React.addons.Perf.stop();
React.addons.Perf.printWasted();

// 2nd render
React.addons.Perf.start();
React.render(<App state=globalState/>, document.body);
React.addons.Perf.stop();
React.addons.Perf.printWasted();

// ... next renders

So as Render takes a callback it seems to make sens to use it instead of considering React.render synchronous.

// 1st render
React.addons.Perf.start();
React.render(<App state=globalState/>, document.body),function() {
    React.addons.Perf.stop();
    React.addons.Perf.printWasted();
});


// 2nd render
React.addons.Perf.start();
React.render(<App state=globalState/>, document.body),function() {
    React.addons.Perf.stop();
    React.addons.Perf.printWasted();
});

// ... next renders

The question that directly comes to mind is what would be the behavior if the 2nd rendering is triggered before the 1st rendering callback has been called?

In an ideal world we would have start / stop / printWasted / start / stop / printWasted but it seem to me that we could also have start / start / stop / printWasted / stop / printWasted which could lead to bad or duplicated mesures, or even worse a failure if Perf does not allow to be started twice for example...

I think my usecase is pretty common for those using React in a really pure way, and it would be nice to explain and document how this can be achieved without expecting weird side effects

slorber avatar Apr 07 '15 15:04 slorber

cc @spicyj @sebmarkbage.

jimfb avatar Apr 07 '15 16:04 jimfb

any idea @sebmarkbage ?

slorber avatar Apr 15 '15 08:04 slorber

nobody has any idea on this?

slorber avatar May 29 '15 15:05 slorber

BTW, Is it possible to call asynchronously a callback passed to React.render? It seems to be called always synchronously.

koba04 avatar Jun 03 '15 09:06 koba04

@koba04 why is it a bad thing? you can still use setTimeout if you need to. Btw I'm not so sure about that, but maybe in simple usecases it's sync? don't know I didn't see that documented but it would be nice to know.

slorber avatar Jun 03 '15 12:06 slorber

@slorber

I just wonder the reason why React.render receive a callback.

The question that directly comes to mind is what would be the behavior if the 2nd rendering is triggered before the 1st rendering callback has been called?

Is it possible to reproduce this? I think this doesn't occur because the callback is called synchronously except using setTimeout.

koba04 avatar Jun 04 '15 01:06 koba04

@koba04 actually it seems you are right. In my complex app the callback seems to be called synchronously.

It's not very clear but I guess React does not guarantee synchronicity in the future as they may change this behavior later? Like if they decide to use something like requestAnimationFrame in the future maybe?

slorber avatar Jun 04 '15 14:06 slorber

Updates happen asynchronously when calling React.render with a new React element on the same container. We might start batching initial renders too.

sophiebits avatar Jun 05 '15 08:06 sophiebits

nice to know. What do you mean by "new react element" @spicyj ?

slorber avatar Jun 05 '15 09:06 slorber

The result of React.createElement or any JSX expression.

sophiebits avatar Jun 05 '15 09:06 sophiebits

@spicyj I wasn't asking for the definition of ReactElement but rather what does it mean to have a new element for a container?

React.render(<Hello/>,node)
React.render(<Hello/>,node)
React.render(<World/>,node)

Do you mean that for the 3st render the callback will be called asynchronously?

slorber avatar Jun 05 '15 13:06 slorber

For the second and third renders there, I believe it's async.

sophiebits avatar Jun 08 '15 18:06 sophiebits

thanks

slorber avatar Jun 11 '15 21:06 slorber

ReactPerf doesn't work in 16 and we'll need to reevaluate the approach before introducing something like it again. I think we can close this since it's specific to the old ReactPerf.

gaearon avatar Oct 03 '17 20:10 gaearon

@gaearon So how can i get Perf results in latest version of react?

rekha110254 avatar Apr 26 '18 03:04 rekha110254