When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Sunday, October 08, 2000

The JavaScript Dictionary Object

By Richard Lowe


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 bonus, can also be used in client-side JavaScript!). (For more information on the Scripting.Dictionary object be sure to read: Using the VBScript Dictionary Object; also, for a unique use of the Scripting.Dictionary, read: Using the Dictionary Object for Data Collection & Validation!)

- continued -

'

The key to improving dictionary performance lies in ASP's other scripting language, JavaScript. JavaScript's implementation allows for objects to be easily created and made available throughout you ASP pages. The real trick is that JavaScript's object properties can be accessed just like a dictionary object:

<%@language=JavaScript%>
<%
  var objAnything = new Object;
  objAnything.Name = 'Richard';

  // output is: Richard
  Response.Write(objAnything['Name']);
%>

Keep adding properties and you have a dictionary object! Of course, there still is the question of how to access these values from VBScript. Since VBScript is still the primary scripting language of the overwhelming majority of ASP developers. So we need to build an interface to the object that VBScript can interact with. We'll build such an interface by creating a JavaScript object. (For more info on server-side JScript objects, be sure to read Richard's past article: Server-Side JScript Objects!)

First, 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 confuses people. 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.

  function mLookup(strKeyName) {
    return(this[strKeyName]);
  }

Second, an Add method is created. This Add method adds key/value pairs to the object, but in a batch way:

  function mAdd() {
    for (c=0; c < mAdd.arguments.length; c+=2) {
      this[mAdd.arguments[c]] = mAdd.arguments[c+1];
    }
  }

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 Scripting.Dictionary object's Add method:

objJScriptDictionary.Add "keyName", "Value"
objJScriptDictionary.Add "keyName2", "Value2"

The best performance is had by calling it like this:

objJScriptDictionary.Add "keyName", "Value", & _
                         "keyName2", "Value2"

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).

Finally, the 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.

  function mDelete(strKeyName) {
    for (c=0; c < mDelete.arguments.length; c++) {
      this[mDelete.arguments[c]] = null;
    }
  }

Assembling the object is a simple matter of adding all these methods in a function called a constructor, like so:

function cCityDiction() {
  this.Add = mAdd;
  this.Lookup = mLookup;
  this.Delete = mDelete
}

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 the Scripting.Dictionary object!

  • Read Part 2



  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article