This would probably be implemented as a tree or something? My point is it needs to be efficient.
I don't know where to find good implementations of data structures for Javascript for something like this, though. I don't want to have to roll my own if I can avoid it.
Help appreciated.