0

If I have a JSON file that let's say represents a dictionary of words, where the word is the key and the value is the definition of that word. Would sorting and organizing the keys in alphabetical order in the JSON file make a difference to the performance when searching a word to find its definition (plus maybe other details about that word) in JS - that is if there were thousands of words or even more?

Or does JSON and Javascript already have an algorithm built in to find results optimally without the need to sort the data for better performance?

Also, I wouldn't mind having an alternative data structure or format or library that could give faster results for this kind of search problem suggested to me! (but of course, this suggestion isn't part of the main question)

C9C
  • 199
  • 2
  • 10

2 Answers2

1

There is no need to sort the keys in and json structure. The reading of a key/value will not be faster after sorting. See the discussion here

If you have very big structures, you can implement a TRIE by yourself. See: Wiki TRIE. Or see this: trie.js

Marc
  • 1,482
  • 1
  • 6
  • 12
1

Sorting the keys does not impact the search performance. Javascript objects may not remember the order of insertion
If possible, use Map object - The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map