JSFiddle - React, Tailwind, and code Playground

by jasonwilczak

HTML

<div id="output"></div>

JavaScript

/*
This approach employs a cache and that's built based on indices (see the 'indexes' const below). Whereas simple.js is
'full table scan' based, this method is analogous to building indexes over this metaphorical table. As with traditional
database design, only the indexes for which queries will be based are actually needed. In this case only one 'index'
needs to be created to accommodate the use cases presented here. Speaking of such use cases, it can be seen that
each respective call is no more than a drill down of another call. The top level call searches by company alone, the
next level call searches by company and job, finally the last call searches by company, job and name. Therefore in the
database world, to optimize a search of this nature, all that's required is a single index / view with
an order by clause consisting of company, job and name (declared in that order). The equivalent of this is done below
to build the cache. In order to accommodate all combination of null / not-null values of each parameters, further
indices would be required. Comments regarding such are provided below.

The cache is built on first call to UserServiceFindUser. It is a tree cache with each node containing a subset of
the data that's appropriate for that node. For each data item associated with a node, all the fields are present. This
of course will require more memory than what's required in simple.js (with the advantage that it should execute more
quickly once the cache is built for successive calls). This memory use can be mitigated by only storing the ID of
each of the records at each node. This would of course mean that once the IDs has been retrieved from the cache, the
actual record associated with that ID would need to be retrieved in post-processing.

*/

let cache = null;


/*
Define the fields as well as how their (normalized) values can be extracted from the record. Values are
normalized for the purposes of comparisons. For job and company, this just means...