Nearly everyone has had a use for a dictionary object at some time. Being able to lookup a value simply by
referencing a string key rather than an ordinal key (as in an array) can be very handy. Currently, the Microsoft-provided
Scripting.Dictionary is the primary method of looking up name-value pairs. In this article, I'd
like to propose a different way that could prove far more efficient for your ASP-based web application (and as a
be sure to read: Using the VBScript Dictionary Object; also, for a unique use of the
Using the Dictionary Object for Data Collection & Validation!)
Lookup is the method to lookup values in our object. This method simply returns the value contained in the
object, referenced by the
strKeyName argument. The
this entity in the function often
this is a reference to the object we will assemble later, not to the function
itself. This function couldn't execute outside the context of the dictionary object we are building.
Add method is created. This
Add method adds key/value pairs to the object,
but in a batch way:
Suffice to say that this method takes every 2 arguments passed to it and turns them into name/value pairs.
Although it's possible to call this function like you'd call the
The best performance is had by calling it like this:
This is because there is overhead making calls between VBScript and a JScript created object. In fact, this
overhead makes this
Add method slower that
Scripting.Dictionary (even the second format),
which is why you probably won't want to use this method, except to adjust certain values. I'll talk about the two
other ways to load your dictionary object below, and why they're faster (and by how much).
Delete method nulls out the values of every key passed to it. Again, this can be slow
and is not the preferred way to do things in VBScript.
Assembling the object is a simple matter of adding all these methods in a function called a constructor, like so:
Now that we've examined the basics of our JScript dictionary object, in
Part 2 we'll look at some
performance tests to see when the JScript dictionary object is more efficient than