Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:00:47+00:00 2026-05-27T07:00:47+00:00

I am building a tree-like data structure out of associative arrays. Each key is

  • 0

I am building a tree-like data structure out of associative arrays. Each key is 1-2 characters. Keys are unique to their respective level. There will be no more than 40 keys on the root level and no more than 5 keys on each subsequent levels of the tree. It might look something like this:

{a:{b:null,c:null},de:{f:{g:null}},h:null,i:null,j:null,k:null}

Initially, I thought that creating so many objects with so few keys (on average, < 3) would be inefficient and memory intensive. In that case, I would implement my own hash table like so:

//Suppose keys is a multi-dimensional array [[key,data],...]
var hash = function(keys){
    var max = keys.length*3, tbl = [];
    //Get key hash value
    var code = function(key){
        return (key.charCodeAt(0)*31)%max;
    }
    //Get key values
    this.get(key){
        //2 character keys actually have a separate hash generation algorithm...
        //we'll ignore them for now
        var map = code(key), i=map;
        //Find the key value
        while(true){
            if (typeof tbl[i] == 'undefined') return false;
            if (code(tbl[i][0]) == map && tbl[i][0] == key) return tbl[i][1];
            else i = (i+1)%max;
        }
    }

    //Instantiate the class
    for (var i=0; i<keys.length; i++){
        var index = code(keys[i][0]);
        while(typeof tbl[index] != 'undefined')
            index = (index+1)%max;
        tbl[index] = keys[i];
    }
}

Then, I read somewhere that JavaScript’s arrays are sometimes implemented as associative arrays when sparsely filled, which could defeat the purpose of making my own hash structure. But I’m not sure. So, which would be more efficient, in terms of memory and speed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:00:48+00:00Added an answer on May 27, 2026 at 7:00 am

    Read this article: http://mrale.ph/blog/2011/11/05/the-trap-of-the-performance-sweet-spot.html

    Basically due to the dynamic nature of JavaScript, your data structures will not be very efficient. If you do need very efficient data structures, you should try using the new Typed Arrays introduced recently.

    If you aren’t into theoretical results, Resig has done real word performance testing on different types of trees looking at data size and performance parsing and processing: http://ejohn.org/blog/javascript-trie-performance-analysis/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a tree like data structure. What is the expected behavior if
I'm building a tree-based data structure and overloaded [ ] so that I can
I'm building a gui component that has a tree-based data model (e.g. folder structure
I'm working on building a tree structure in MySQL and have been experimenting with
I'm building a menu using Prism (using a trtelerik tree view with hierarchy data
I have an LDAP subtree, whose entries correspond to a tree-like structure of Java
I'm building a tree structure (or rather modifying one of the examples with a
I have a 'hash tree' like this: dat = { 'building' => {'street' =>
I have a data structure that represents C# code like this: class Namespace: string
I'm building an app with D3.js that displays a data in a tree-map. Ideally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.