Home > OS >  Why is Typename function so slow with Dictionary Class?
Why is Typename function so slow with Dictionary Class?

Time:11-14

My fellow VBA-elitists,

during my current Project i stumbled upon a problem with Dictionaries. Im using the vba JSON Parser by Tim Hall that uses dictionaries to translate to json.

While digging into performance issues with nested dictionaries that have large first nests i identified the bottleneck in the TypeName call:

    If VBA.TypeName(JsonValue) = "Dictionary" Then ...
   

I Tested the typename with different types and none seems to be nearly as slow as a dictionary. For example 1000 typename(dictionary) calls take about 400 milliseconds, whild typename(string) calls take like 2(!) milliseconds.

I tested other Objects aswell like Word Documents, Collections and other dll library objects like the RegExp Object, all about 2 milliseconds.

Any Ideas? Thanks <3


PS: I wrote my own little isDictionary Method, that checks if "dictionary.CompareMethod" is callable, interestingly with this method only the FIRST lap of the test now was 400 ms while the other 4 were 2 ms with now the bottleneck being when an Item of the Dictionary was called by the "Dictionary(Key)" Syntax.

It was nearly exactly as slow as if i had used the TypeName function, but only in the first lap :D

CodePudding user response:

I can't answer the Why, but as an alternative, and only if you are Early Binding to Scripting, it is faster to use TypeOf rather than TypeName

Eg

Function IsDictionary(o As Variant) As Boolean
    On Error Resume Next
    IsDictionary = (TypeOf o Is Dictionary)
End Function

The Error Handler is to ensure IsDictionary returns FALSE if it's passed a non-object type

  • Related