DEV Community

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

 
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 • 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...