I have ~1Kb dict looking like:
db = {"word": 0, "something": 1, "another": 2, ..., "last": 36000}
and I use the following operations:
- find an index of some word (like
db["something"]
); - find word by its index (like
Object.keys($db)[1234]
)
It works slowly. Would it help if I switch to array?
2
Answers
jsben.ch shows much faster performance with array:
compared with dict:
I would say a list of words with length 36000 is not big enough for any optimizations if it is local and in-memory, so just the plain array if it works.
Array.prototype.indexOf
andArray.prototype.find
are surprisingly fast on modern devices.If you really need to optimize, in javascript, you may want to use two maps, one is
Map<string, number>
, the other isMap<number, string>
.Or you may just use, like, indexedDB, sqlite, Redis, or one of those databases.
Btw, your implementation to map a number to its word is wrong and it might not always get the correct result on all engines. You should look up the real number instead. Using
for...in
should slightly improve the speed as well: