Skip to content

Instantly share code, notes, and snippets.

@kranfix
Last active January 9, 2022 16:07
Show Gist options
  • Save kranfix/d7625a4abf9b5ece934879609cb53c4f to your computer and use it in GitHub Desktop.
Save kranfix/d7625a4abf9b5ece934879609cb53c4f to your computer and use it in GitHub Desktop.
const memory optimization in Dart
void main() {
final obj1 = MyComplexObject(1, 2.0);
final obj2 = MyComplexObject(1, 2.0);
assert(!identical(obj1, obj2)); // obj1 and obj2 are not the same object
const cobj1 = MyComplexObject(1, 2.0);
const cobj2 = MyComplexObject(1, 2.0);
assert(identical(cobj1, cobj2)); // cobj1 and cobj2 are the same object
assert(!identical(cobj1, obj1)); // cobj1 and obj1 are not the same object
}
class MyComplexObject {
const MyComplexObject(this.a, this.b);
final int a;
final double b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment