- Home /
Optimising dictionary load (in Javascript)
I have a TextAsset, dictionary
, which is a file containing a word per line, representing a scrabble-style word set. Currently I am iterating over it, like so:
var words = dictionary.text.Split("\n"[0]);
validWords = new Hashtable();
for (word in words) {
validWords[word] = true;
}
But the file is around 3MB and therefore takes several minutes to load. Ideally I would like to be able to store the dictionary contents in a lightweight binary format that could be loaded "in place". Is this possible at all?
Does anyone have any other suggestions?
Answer by Meltdown · May 14, 2011 at 11:51 AM
I'm not sure if this will be relevant to your project but have you tried splitting the word file into a seperate file for each letter of the alphabet. i.e A.txt, B.txt etc.
Then if you are randomly getting a word, first pick a random letter from the alphabet. Then open that specific word file.
Alternatively if that doesn't work for you, have you looked into using a database engine of some sort?
That's an interesting idea, to sort of pre-hash the hash, but I'd really like to have the whole thing cached in memory at startup time, so that I can quickly query it during gameplay.
hi @gosub, i am also trying to implement dictionary.did you get solution from dictionary? if You know please share your idea.
Answer by Eric5h5 · May 14, 2011 at 02:01 PM
Just to focus on one aspect, using Dictionary would be rather faster (and more typesafe) than Hashtable:
var words = dictionary.text.Split("\n"[0]);
validWords = new Dictionary.<String, boolean>();
for (word in words) {
validWords[word] = true;
}
I neglected to mention that I am coding in Javascript and afaik there is no support for the Dictionary
type in JS.
@gosub: I know you're using JS; it was clear from the code you posted. I wouldn't have written my answer if there wasn't support for Dictionary in JS. (Assu$$anonymous$$g you're using at least Unity 3.0.) Use "import System.Collections.Generic" when using generics (List, Dictionary, etc.).
Thanks for the clarification @Eric5h5. I'm new to unity and don't know .NET or C# at all, so there are a lot of unknown unknowns for me atm. Please forgive my ignorance.
@gosub: see the docs here: http://msdn.microsoft.com/en-us/library/ms229335(v=VS.90).aspx None of the $$anonymous$$icrosoft.* stuff would apply, of course, as well as some things like System.Drawing and System.Web, but most of the more generic stuff works, like text, regular expressions, I/O, xml, etc.