1

I'm checking whether a word is real or not by referencing an object that looks like this:

dictionary.js

var dictionary = {
   "apple" : "apple",
   "banana" : "banana",
   "orange" : "orange"
}

In JavaScript, because of the keys, the checking is "instant" (I forget the name of the lower-level phenomena in data storage that makes this possible) (hash table), rather than looping through the whole object to find a match:

if (dictionary["apple"]){
    //word is valid
}

But what bothers me about this is that each word is listed twice, when the value of each property's key is never used. The property's key (name) is the only thing checked, with the value being redundant. This bothers me because for one thing, redundancy in programming looks very bad, and also this dictionary array holds over 100,000 words, and this object which must be loaded to the page (page load time) and then stored in memory (RAM being utilized) as an object is twice the necessary size.

My first idea is to just remove the values:

var dictionary = {
   "apple" : "",
   "banana" : "",
   "orange" : ""
}

But that looks weird. So, my question is:

Is there a better way to do this?

  • 1
    I guess this is related: http://stackoverflow.com/questions/26833922/how-can-i-load-a-long-list-to-be-converted-into-an-array-in-stages-rather-than (same code, different question) –  Nov 10 '14 at 02:01
  • Why not `{"apple" : true}`? Theoretically, a Boolean value is just one bit. – Felix Kling Nov 10 '14 at 02:04
  • "because of the keys, the checking is instant", do you mean like in a hashtable? – Sergey Nov 10 '14 at 02:05
  • @Serg Exactly :) That was the word I was looking for. –  Nov 10 '14 at 02:08
  • why does it have to be a key=>value store, why can't you just have an array of allowed words (sorted or hashed) and check each word against that list, e.g., allowed_words.indexOf(suspect_word); – Sergey Nov 10 '14 at 02:09
  • @FelixKling that looks like a good idea.. I wonder if an empty value is remembered as more than 1 bit?? –  Nov 10 '14 at 02:09
  • @Serg because the `key=>value` store is what utilizes the hash table. If I remember right, `indexOf` loops through to find the item - that would be far, far more expensive. Does `indexOf` utilize a hash table also? –  Nov 10 '14 at 02:10

2 Answers2

1

You may want to try this:

  function Dictionary()
  {
    this.apple = 
    this.banana = 
    this.orange = true;
  }

  var dictionary = new Dictionary();

  console.log(dictionary.apple);
  console.log(dictionary['banana']);
  console.log(dictionary['not an orange'])
  console.log(dictionary.notaword);
Isaac
  • 10,133
  • 5
  • 27
  • 43
  • Whoah, is that shorthand? It actually works, I just ran it, but I've never seen `= ` used like that. –  Nov 10 '14 at 02:32
  • @CuriousProgrammer: It's just one big expression, evaluated as `this.apple = (this.banana = (this.orange = true));`. It may look strange because it's split over multiple lines. Nothing special, not shorthand. – Felix Kling Nov 10 '14 at 04:58
  • @FelixKling My curiosity wasn't patient enough ;) http://stackoverflow.com/questions/26836921/what-is-this-weird-javascript-shorthand-and-why-does-it-work#26836991 –  Nov 10 '14 at 05:00
0

You could get a little leaner with

var dictionary = {
   "apple" : true,
   "banana" : true,
   "orange" : true
}
Oakley
  • 606
  • 10
  • 21