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

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Sunday, October 08, 2000

The JavaScript Dictionary Object, Part 2

By Richard Lowe

  • Read Part 1

  • In Part 1 we looked at the basics for our JScript dictionary object. In this part, we'll look at some performance tests to see when it is wiser to use our JScript object as opposed to the Scripting.Dictionary object!

    - continued -

    Now for the speed part. All this, of course, is just a mental exercise unless some performance and/or functionality benefit is gained. There are two scenarios to get really stellar performance out of JavaScript dictionary objects. The first is to load the dictionary in JavaScript. The simplest (and fastest) thing you can do is directly load values into the constructor object we just built. This is the syntax:

    <script language=JavaScript runat=Server>
      function cCityDiction() {
        this.Add = mAdd;
        this.Lookup = mLookup;
        this.Delete = mDelete
        // Load Values:
        // ...
        this["Pensacola"] = "FL";
        this["Peoria"] = "IL";
        this["Pepperell"] = "MA";
        this["Peraland"] = "TX";
        this["Peterborough"] = "ON";
        this["Phoenixville"] = "PA";
        this["Piedmont"] = "OK";
        this["Pilot Point"] = "TX";
        this["Pine Bush"] = "NY";
        this["Pittsford"] = "NY";
        // ...
      // This function exposes the Cities dictionary object to VBScript:
      function Cities() {
        var o = new cCityDiction();

    In the example file titled JSDictionaryCities.asp I have loaded 788 city names along with their cooresponding state or county code. This is the include page that builds the JavaScript dictionary, the timed test is in TimeIt_JS.asp. This loops through the process of loading the 788 items 100 times (78,800 loads). The same test is repeated in TimeIt_SD.asp for the Scripting.Dictionary object. Running TimeIt_JS.asp returns this:

    Timing native JavaScript / VBScript Dictionary:
    Adding 788 Items (repeating 100 times):
    Total Add time: 331ms
    Avg Time: 3.31ms

    Looking Up 100 Items (repeating 100 times): Total Lookup time: 511ms Avg Time: 5.11ms


    This shows that the total time for loading 78,800 items is 331 ms. Looking up 10,000 items took 511 ms. Running Compare to TimeIt_SD.asp returns the following:

    Timing COM Scripting.Dictionary:
    Adding 788 Items (repeating 100 times):
    Total Add time: 2163ms
    Avg Time: 21.63ms
    Looking Up 100 Items (repeating 100 times):
    Total Lookup time: 201ms
    Avg Time: 2.01ms

    The total and average Add times for Compare to TimeIt_SD.asp are much higher. However, the overhead in our .Lookup function causes performance on the JavaScript object to drop, and 10,000 lookups only take 201 ms on the Scripting.Dictionary side.

    After trying combinations of numbers of name/value pairs and numbers of lookups, you can still get better efficency out of 20 name/values pairs and 15 lookups from the JavaScript object. If you have less items and more lookups than that, use Scripting.Dictionary.

    The second case in which you can speed up the JavaScript dictionary implementation is when you wish to load dictionary values from a database. Due to the fact that you can load the JavaScript dictionary object with only one .Add method call, you can use the .GetString method of ADO's Recordset object to efficiently get the data from the database and populate your Dictionary!

    The method here is to add the the Cities() function that returns the dictionary object to VBScript. From JSDictionaryCitiesDB.asp:

    function Cities() {
      var o = new cCityDiction();
      // Load from a db each time:
      var strAddValues = new String('');
      var strConnection = 'Provider=SQLOLEDB; Data Source= mfrank; ' + 
                          'Initial Catalog= CityStates; User ID=sa; Password=';
      var objConn = Server.CreateObject('ADODB.Connection');
      var strSQL = 'SELECT RTrim(city), RTrim(state) FROM CitiesAndStates';
      strAddValues = '"' +
           objConn.Execute(strSQL).GetString(2, -1, 
                       '","',  '","', ' ') + '", ""';
      eval('o.Add(' + strAddValues + ');');
      objConn = null;

    This function creates a new dictionary, then passes a very large string from the data base to the .Add method using the eval() function. The .Add function then takes care of adding all the values in. By running TimeIt_JS-DB.asp you see the following:

    Timing native JavaScript / VBScript Dictionary:
    Adding Items (repeating 10 times):
    Total Add time: 621ms
    Avg Time:62.1ms
    Looking Up 100 Items (repeating 10 times):
    Total Lookup time: 60ms
    Avg Time:6ms

    The average time is 62.1 ms instead of the 3.31 ms for a load of the same number of hard-coded values, but for a database load, it's pretty quick. Here are the results from TimeIt_SD-DB.asp:

    Timing COM Scripting.Dictionary:
    Adding Items (repeating 10 times):
    Total Add time: 1161ms
    Avg Time:116.1ms
    Looking Up 100 Items (repeating 10 times):
    Total Lookup time: 20ms
    Avg Time:2ms

    Again, the load time is quite a bit higher: 116.1ms per 788 items vs JavaScripts 62.1ms. Well, hopefully these performance gains possible with JavaScript Dictionary objects will help you and your applications achieve maximum performance.

    Happy Programming!

  • By Richard Lowe
    Richard Lowe works as a Development Services consultant for Spherion in the Technology Group

  • Download the support files (in ZIP format)
  • Read Using the Dictionary Object for Data Collection & Validation
  • Read Using the VBScript Dictionary Object

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