Search code examples
performancevbscriptdereference

Performance impact of With statement when referencing object instance variables and properties?


Do you guys know how much of an impact a With statement for object instances has when accessing an object instance's attributes and properties? How "expensive" are fully qualified object references?

Being too lazy to check for myself by benchmarking this, I'd like to ask it here; maybe someone has benchmarked that before.

I am considering stuff like

Dim ObjectInstance
Set ObjectInstance=New MyClass

With ObjectInstance
  SomeField=Value
  SomeProperty=Value
End With

versus

Dim ObjectInstance
Set ObjectInstance=New MyClass

ObjectInstance.SomeField=Value
ObjectInstance.SomeProperty=Value

And of course I might have tens or hundreds of assignments. And I might nest With statements if a class references an instance with an instance variable.

Other way of asking:

Is it correct that using a With statements dereferences the object instance ONCE while the fully qualified reference needs to dereference everything again and again?

Note we are talking about Visual Basic Scripting Edition, sub-sector Visual Basic Scripting Host, which is a very late-binding (too late-binding? :-) ) interpreter language.


Solution

  • Ok I did not do exact measurements. However, I can see that hundreds and hundreds of fully qualified assignments take almost no time at all, less than a tenth of a second, on my machine while other stuff takes much, much longer.

    So as long as the property setters involved are rather trivial, I'd never create a With statement solely for performance reasons.

    In other words: A counter example are QTPs test objects. Doing a With makes sense here more often because each and every reverence of a test object might result in QTP looking up the GUI object in the AUT GUI. Here, With'ing might make sense. But this becomes so obvious once you need the With because things get real slow then.

    So all this is still is an acknowledgement of the rule:

    Don't optimize prematurely.