Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a performance gain for final variables?

Tags:

dart

Yeah, the title. Is there any difference (other than design, obviously) when writing final or not?

like image 378
Levi Lesches Avatar asked Aug 23 '19 22:08

Levi Lesches


2 Answers

As Flutter framework is build around immutability, the final keyword is a way to enforce that immutability. So it is help ensure the following of Flutter design patterns.

In other hand , the Dart compiler do some optimizations that would not be possible if variable was marker var

like image 32
bwnyasse Avatar answered Sep 28 '22 02:09

bwnyasse


Both "final" and "const" keywords may improve performance and reduce APK size for applications built in Flutter. "const" Widgets have a different lifespan that improves performance.

Using these keywords indeed does some good. Though final specifically may be misleading, and the compiler often knows the variable is never reassigned without the keyword. The improvements might not be worth the time. It is a matter of personal taste

I personally stick to using them, because no matter how small it seems, all performance issues are known to stack up and snowball.

like image 138
Alexey Subbotin Avatar answered Sep 28 '22 03:09

Alexey Subbotin