DEV Community

loading...

Discussion on: Flutter Performance Tips (#1): const constructors

Collapse
pedromassango profile image
Pedro Massango Author

The benefits is that the widgets declared as const won't get rebuilded when you use setState().

Collapse
remejuan profile image
Reme Le Hane

You mentioned that, but what is the actual benefit, is the app gonna be faster, smoother. What is the benefit for the dev or the end user in a text element not rerendering?

The exact same argument was made for the example. I mentioned, but again if it not rendering saves you 0.0000001ms on render the benefit is practically non existent and it's over optomization at the cost of readibilty with no real world benefit.

Thread Thread
tatsuukraine profile image
Denis Beketsky

Benefit of using const is that such items will be compiled once during AOT

Thread Thread
remejuan profile image
Reme Le Hane

Still not answering the ACTUAL question. So what if it renders twice, is it going to slow things down, blow up the users phone, what.

We adding a bunch of words to decrease code legibility for what actual reason.

Once vs twice vs 100 times what does it actually matter. Sure in a blog post it sounds like a good idea to not waste renders, but in reality a render works from the top down if the engine is optimal it will know the text element has not changed and carry on with its job to do a dif will take a fraction of a millisecond.

You get new devs coming to this, taking the word as gospal as they do not have the experience to think 'why' and end up with bugs and side effects as they have no idea why they doing it or if they even really should and they can't know if they should unless they know the actual why the actually benefit, the actual reason why this is better than not doing it, what is this going to do to improve the user experience, what is this going to do to my apps performance and more importantly when they should not do this which can hopefully be inferred from the actual reason why.

People seem to forget there is such a thing as over optomizing, something very easily achieved when you accept 'because I said so' as a valid and ultimately, unquestionable reason.

I find it shocking that I am the only developer not finding some random blog post online as gospel truth. Maybe we should do it, maybe we should not, when do we and when don't we and why why why are all questions not being answered.

Thread Thread
tatsuukraine profile image
Denis Beketsky

I think it's just a question of overall optimization. Using const allows you to minimize memory allocation, also it allows you to define such items only once during AOT compilation. It's definetly not improve your app performance significantly, but using const will make it a little bit faster. If you have for instance 100 items probably this const optimization won't make huge performance boost. You can search across internet how dart treats with const variables. For example you can take a look in official docs about StatelessWidget under Performance considerations section

Thread Thread
pedromassango profile image
Pedro Massango Author • Edited

Lets say that it is a small performance improvement, but it can add up in larger apps or apps where the view is rebuilt often for example because of animations.
const reduces the required work for the Garbage Collector.

(Updates)
Read more:
api.flutter.dev/flutter/widgets/St...