When creating a widget tree, will inserting const
before static widgets improve performance?
ie
child: const Text('This is some text');
vs
child: Text('This is some text');
I know that, with Dart 2, const
is optional and will be inserted automatically is some places. Is this one of those situations? If it isn't, will using const
reduce memory usage/improve performance?
Thanks for your answers!
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.You can enable some linter rules in
analysis_options.yaml
that tell you when you should addconst
because it's not inferred but would be possible likeor that reminds you when you use
const
but it is inferred anywaySee also https://www.dartlang.org/guides/language/analysis-options
I've ran some test to see if it makes a difference.
The tests are heavily based on the ones done in this article.
For the tests, there are 300 containers with text inside moving randomly on the screen. Something you wouldn't see in a day to day app.
For my results there is no difference in frame per second and there is no difference in memory usage except that the Garbage collector seems to run more often when not using const. Again, the FPS were about the same.
Imo, the performance boost is negligible and sounds like preemptive optimization. However there is no deeply nested chain of widgets in the test, but I don't see how that would make a difference. The article above seems to say there is a small one, but that's not what my tests show.
I've a card like this (this is the const version):
that's rendered 300 times and moving on the screen randomly.
This is the widget that makes them move
Note: I'm new to flutter, and so are many others because it's a relatively new framework. Therefor my tests could very well be wrong, don't take it as gospel. Also don't take it as gospel when you read an article titled << Number One Perf gain on Flutter >>. I've yet to see actual proof there is a perf gain. And preemptive optimization is a sweet fallacy to fall in.
In the case of Flutter, the real gain with
const
is not having less instantiation. Flutter has a special treatment for when the instance of a widget doesn't change: it doesn't rebuild them.Consider the following:
In the case of
build
method being called again (setState
, parent rebuild,Inheritedwidget
...), then due to theconst
forBar
subtree, onlyFoo
will see itsbuild
method called.Bar
will never get rebuilt because of its parent, because Flutter knows that since the widget instance didn't change, there's nothing to update.