1

I'm currently designing a class structure for a project I'm working on. I have a method that uses one instance state. I don't now wheter it's better to make this method static and parse this instance state as an argument or just tie the method to the instance.

If performance was no issue I would tie the method without any doubt to the instance, because it's much cleaner that way. But in my case performance will be really crucial. So, does it make any difference performance-wise to make the method static / non-static?

If it makes no difference, will that be true for the generated *.dart.js javascript aswell?

Edit: After reading my own question it's not really coherent. I will try to formulate it again, but clearer.

This code ...

class MyClass {
  void foo() {}
}

void main() {
  MyClass a = new MyClass();
  MyClass b = new MyClass();

  print(a.foo == b.foo);
}

... outputs false. This make me think that for each new instance a new method is created. If that is true this seems to me as a waste of memory. So, does each new instance create a copy of all it's bound methods?

PS: The question is basically the same as this question, but then for Dart.

Community
  • 1
  • 1
Tomasito665
  • 1,188
  • 1
  • 12
  • 24
  • I have no clue what this question is about. I guess it would be easier to understand with some actual code that demonstrates what you try to accomplish. If performance is important to you, it should be easy enough to write a benchmark and get hard data. – Günter Zöchbauer Dec 06 '15 at 06:42

2 Answers2

3

No, creating two instances doesn't duplicate the methods. Methods are like static functions where the object instance is passesd as argument with the name this.

Don't worry too much about performance before you run into actual performance issues especially at such micro-level. Usually performance isn't a matter for the bigger part of your applications code base because most of the code is usually run very seldom. When you run into performance issues you can investigate and find the real hot spots that are executed often enough so that optimization actually makes a difference.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
2

Dart classes don't have different methods for different instances. There is only one method per class.

Extracting a function creates a new function object every time you do it, and those objects may or may not be equal depending on which function you extract from which objects:

class MyClass {
  void foo() {}
}

void main() {
  MyClass a = new MyClass();
  MyClass b = new MyClass();

  print(a.foo == b.foo);  // False. 
  print(a.foo == a.foo);  // True
  print(identical(a.foo, a.foo));  // False!
}

When you perform a method extraction from an object, you create a new object. The new object is a "closure" which contains the function to call and the object to call it on. Two such closures are equal (according to operator==) if they refer to the same function on the same object. That's why a.foo and b.foo are not equal - they are equivalent to () => a.foo() and () => b.foo() respectively, and since a and b are not the same object, the function objects are not considered equal.

lrn
  • 64,680
  • 7
  • 105
  • 121