When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

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


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



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES