Perhaps a hidden but yet powerful usage of a combinations of collection is underrated in many software solutions, where search operations are performed by iterating collection items one by one, which might hurt performance in some cases.
For example, in JavaScript and ECMA-Script 5.x standard onwards, Array’s prototype includes functions like filter or find to filter out items within an array:
Also, imagine that you would want to get an ordered array of persons ordered by their age:
Above approaches are fine when we work with tiny collections, but once you need to work with collections of 1000, 10,000 or more items, these would turn into a performance bottleneck.
Solution
In many languages like JavaScript where functional programming style have been introduced to simplify a lot of kinds of iterations, it sounds cool to use them everywhere, but as have been stated on What does it solve?, cool things aren’t always a good friend of optimal performance.
An approach to avoid such performance bottlenecks could be a simple indexing infrastructure. Let’s use JavaScript as a sample, but it would also work in many other languages like C#, Java, Scala, and even NoSQL database technologies like Redis (read the comments to understand the approach):
Also, taking the whole index object, just imagine that we own a person id and
we want to know if the whole person is older than 30. We wouldn’t need to get
the person from index.persons and check if person.age > 30, but it would be
as easy as (read the comments to get further information):
Highlights of this approach
It’s applicable to most modern programming languages and even NoSQL databases like Redis.
Objects or data is retrieved very fast even with large collections because they’re not iterated anymore to search or extract objects or data.
Some boolean checks become easier and more readable.
As a con, some indexing logic should be implemented and objects or data should be indexed carefully.
Did you find some problem on this design pattern, or do you want to improve it?
Go to our GitHub repository and open an issue, and let's talk about it!