DEV Community

Discussion on: Optimizing Search in JavaScript

Collapse
 
bgadrian profile image
Adrian B.G.

Nice, you have built an Index, transforming a Search operation into a Lookup.

As a note you cannot trust/use the peopleMap.length, because most likely the IDS will not be incremental starting from 0, so it will become a "sparse array" and the length will be the max(id) +1. Also if the first element of the map will be a String, and not a Number, the map will be transformed into an Object and the length property will not exists.

In this case you have to be careful to not use String (text) properties for the index, when the storage is an array (like in your case).

For simplicity I would make const peopleMap = [] an object, and rename it to improve the readability: const peopleByID = {}. If you use numeric values I think it will be still transformed to a sparse array so [] or {} does not matter, but it will enforce the dev to NOT use the .length and other Array functions.

Multiple indexs can be added in the same way, one for each column to be searched.
If the values are not unique, the values will be a list of elements.

const peopleByAge = {};
peopleByAge[34] == [
 {"id":24,"name":"Justin","age":34}, 
 {"id":2,"name":"Rebecca","age":34}
]"

I have found rare occasions in practice for this kind of optimization, usually the data is too big or too expensive (hard to keep it up to date) to cache it locally, and usually is stored in a key-value store or directly in DB,
... but if you have this situation, where you do multiple searches on the same data set, the trade-of (between using extra memory for the index) and the CPU savings worth it.

Collapse
 
dev3l profile image
Justin L Beall

The data set did become quite large, I had to add --max-old-space-size=4096 as a node parameter! I try to store only reference data in this cache, but even that is a lot.

The purpose of the cache is to limit network IO of a data conversion, more memory overhead less IO. But, you are absolutely right. If the data cache became unmanageable, I would push the data out of memory into a local key/value store.

I like the idea of using an object instead of a list!