When a search box became an architecture problem
There is a dangerous stage in building software where a feature starts looking finished.
The search box worked. Type something, get a few experiments, click one, move on.
For a while, that was enough.
Then VLabs grew.
More semesters. More subjects. More experiments. And, more importantly, much more content inside every experiment.
At some point we had to ask a slightly uncomfortable question:
What does it actually mean for a student to search a virtual lab?
If they remember the name of an experiment, the old search works. But what if they remember a sentence from the theory? A procedure step? A concept they saw somewhere but don’t remember which experiment it belonged to?
That was the point where search stopped being a small UI problem.
Dropdown over title/description → full-text ⌘K command palette → index moved out of the browser and into the build step → a stray findIndex() fixed → hyphen-insensitive, order-independent word matching → a debounce to stop live 3D previews from fighting the user’s typing. Same feature, five different kinds of problem.
The search we started with
This was the original search.

It did one thing: match an experiment and show it in a dropdown.
Type HALF and you get Half-Wave Rectifier, Half Adder, Half Subtractor, and a few other things that happen to contain the same word.
Which sounds reasonable until you look at what the result tells you.
Not much.
- No theory snippet.
- No procedure step.
- No indication of where inside the experiment the match came from.
- No preview of the circuit.
Just a title, a subject, and a few tags.
The search knew what an experiment was called.
It didn’t know anything about the experiment itself.
And the implementation was almost as simple as the UI.
The search worked against ALL_EXPERIMENTS, which carried basic metadata for each experiment — title, description, tags and circuit ID. Matching was essentially a collection of includes() checks across that array.
That was perfectly reasonable when the question was:
“Does this experiment look like the thing I’m searching for?”
We were starting to ask a different question:
“Where does this concept appear anywhere inside the lab?”
We wanted search to understand the labs
The next version wasn’t supposed to be just a nicer input box.
We wanted search to become a way of navigating the entire lab.
The target was a proper ⌘K command palette — something closer to the search experience you’d expect from a documentation platform than a dropdown attached to a navbar.

Suddenly the search had room to tell us more.
A result could now show its semester, subject and experiment. It could point to the exact section where the match occurred and show a snippet of the content around it.
We also added experiment preview cards, semester filtering, highlighted matches, and a keyboard-first flow you could open with ⌘K.
But the UI was only the visible part of the change.
The real requirement underneath it was much bigger:
Search shouldn’t just find experiments. It should find things inside experiments.
That meant search needed access to the actual lab content.
Theory. Procedure steps. Descriptions. The text a student actually reads while doing an experiment.
Which meant we needed to build an index.
The first index worked a little too well
The first implementation was straightforward.
We created a SearchIndexEntry shape, flattened the experiments and their content into a searchable array, and let the command palette search through it.
It worked.
And then we looked at what “working” actually meant.
To build that index in the browser, the browser needed access to the actual lab content.
Every semester. Every subject. Every experiment.
Every theory paragraph and procedure step we wanted to make searchable — walked, parsed and flattened, in the user’s browser, before a single character had even been typed.
We had solved the search problem by creating a bundle problem.
The search was better.
The architecture wasn’t.
This was the first real optimisation decision: the browser didn’t need to know how the index was built.
It only needed the finished index.
We didn’t land on that by staring at a flame graph. Shubham — VLabs’ founder, and the person every weird bug eventually reaches — said it first, in two messages at 3:34 and 3:39 in the morning:
“so, we’re building this semantic search most of the TEXTS. but the search gets through too many file systems at once when fetching things. client can get exhausted.”
“somebody must work towards optimizing these things. option is maybe to — dump all semantics in a single file — always let that ONE file be fetchable by the search and links inside that itself. rather than going inside of ./01-analog-electronics/02-zener-diode.., ./01-analog-electronics/03-e.., ./01-analog-electronics/04-....”
“we can just ./search-build-output.ts fetch a single file. something like this. not sure where should lie tho?”
“and this should be a BUILD method. on run build, some build files → outputted → helps in semantic searches. in this way devs don’t have to optimize for every change they do and it’s already in the npm run build commands on deps + builds.”
That’s the entire architecture decision above, typed into a chat before any of us had opened an editor. Strip out the punctuation and it’s exactly what shipped: one fetchable output file, generated on npm run build, so nobody has to remember to re-optimize search every time they add an experiment.
Build the index before the browser arrives
So we moved the expensive part out of the browser entirely.
A Node script now runs during both the development and production build process. It imports the real SEMESTER_CONTENTS, walks through the lab content, and generates the searchable index ahead of time — before any user ever opens the page.
The generated result lives in search-index.output.ts. The client doesn’t import the individual lab content modules anymore — it gets a flat array that’s already prepared for searching.
The important part is what disappeared from the browser:
- No walking through semester folders.
- No loading every theory and procedure module just to construct an index.
- No flatten-and-group pass waiting for the user to open search.
The expensive work of understanding the lab structure belongs in the build pipeline. The browser should receive the thing it actually needs: a searchable index, not the means to construct one.
Then localhost started feeling slow
Moving the index out of the client solved the bigger architectural problem.
But the search still didn’t feel perfect during development.
The first thing we had to separate was development performance from production performance. next dev is not a production build — React development behaviour, unminified code, and missing production optimisations can make the same interaction feel very different from what ships.
But there were also real problems in the code.
One of them was particularly easy to miss.
There was a findIndex() sitting inside the result rendering loop — called once per header row, and again per snippet row, on every single render.
// before — re-scans the whole list for every row, every render
const idx = flatItems.findIndex((f) => f.entry.id === s.id);// after — built once per render, looked up in O(1) per row
const indexById = useMemo(() => {
const m = new Map<string, number>();
flatItems.forEach((f, i) => m.set(f.entry.id, i));
return m;
}, [flatItems]);
const idx = indexById.get(s.id) ?? -1;We weren’t doing anything useful with those repeated scans. The lookup could be computed once and reused.
A small change, but exactly the kind of work that disappears once you stop looking at the search algorithm and start looking at everything around it.
Then search failed in a much simpler way
After all of that, we found a problem that had nothing to do with bundles, rendering, or WebGL.
Search for:
half wave
And the search could fail to find:
Half-Wave Rectifier

Shubham found it before I did, and said so plainly:
“regex pipeline and semantic search can be improved, allocate it to someone else to apply better semantic search on the website.”
“and, make it fast. there must be a way.”
“i kind of am curious about how the semantic search is being built on client.”
Fair on all three counts — the regex pipeline genuinely could be improved, and the honest answer to “how is it being built on client” was, at that point, “more than it should be.” That question is what turned into the normalisation and token-matching work below.
The computer was technically correct.
The strings were different.
Humans don’t care.
Making search understand how people type
We stopped comparing the raw strings.
The new normalisation step converts separators — hyphens, underscores, dots, and whitespace — into a flexible regular expression symbol that matches any gap.
Now all those different spellings will successfully match against the same query.
Because the regex allows any combination of these separators between words, punctuation doesn’t get in the way.
Search starts behaving less like a strict string comparison and more like what a person expects when they type a query.

Finding the match is only half the job
Once matching became token-based, the highlighting had to understand the same rules.
Highlighting only the first occurrence wasn’t enough anymore. If the query contained multiple words, every relevant token in the result snippet needed to be visible.
buildHighlightRegex() builds a pattern from the individual query tokens so a result can highlight each matched word, not just the one the old code happened to find first.
It’s a small detail, but it changes the experience quite a bit.
The user shouldn’t have to read an entire paragraph just to figure out why it appeared in the results.
Then I put Three.js inside the search box
This is where things got slightly funny.
The result cards looked better with circuit previews, so we added live CircuitPreview components.
Which meant that typing into a search box could now change the result set, which could mount or unmount multiple WebGL canvases.
A search result had somehow become a small rendering application.
The previews looked great.
They were also not something we wanted rebuilding on every character.
We added a small debounce — about 180ms — so matching, grouping, and preview generation wait until the user pauses typing.
The idea isn’t to make search feel slower. It’s to avoid doing work the user is actively changing their mind about, before the result is even ready to be seen.
What the search became
By this point, the search had changed in almost every layer.
It started as a dropdown that searched experiment metadata.
It became a command palette backed by a build-time index containing content from the actual labs.
A student can open it with ⌘K, type a phrase, filter by semester, and see exactly where the match came from.
The result isn’t just:
Half-Wave Rectifier
It can tell you:
And underneath that path, it shows the actual piece of content where the match was found.

The search box wasn’t really the thing we were optimising
Looking back, the interesting part wasn’t the ⌘K interface.
That was the easy part.
The harder problem was deciding where search should live.
We started with a client-side substring check because there wasn’t much to search. Then we wanted search to understand the actual lab content, which made the client-side approach expensive.
So the index moved to build time.
Then the UI exposed unnecessary rendering work. Then live circuit previews introduced WebGL into an interaction that needed to stay lightweight.
And after all of that, a hyphen reminded us that good search isn’t only about performance.
It also has to understand what the person meant.
A search box started as a small dropdown with an includes() call.
It ended up touching the content architecture, the build pipeline, the browser bundle, the rendering lifecycle, input scheduling, and text normalisation of the entire platform.
That’s probably the slightly annoying thing about optimisation.
You rarely optimise the thing you thought you were going to optimise.
You start with a search box.
Then you discover you’re really optimising the system behind it.