Menu

Three caches answer a barcode scan, and the only one with an expiry has nothing to do with speed
📰

Three caches answer a barcode scan, and the only one with an expiry has nothing to do with speed

DEV Community: typescript·Daniel Pertu·about 21 hours ago
#R3TQZhJu
#devtotypescript#devto#dev#scan#layer
Reading 0:00
15s threshold

Scanning is the one thing in Munchable that has to feel instant. You are standing in a shop holding a packet, you point the camera at the barcode, and the answer about whether that product suits you should already be there. Everything else in the app can take a moment. This cannot.

So the scan path has three caches and a de-duplicator in front of one network call. They look like one optimisation. They are four different decisions, and only the first is about speed.

Layer 1: a Map, read synchronously

Products resolved during this session live in a plain Map , and the scan path peeks at it synchronously. No await , no storage round trip, no state update. Scan the same packet twice while comparing two brands on a shelf and the second scan costs nothing at all.

The peek has one rule that took a bug to find:

export function peekProduct ( barcode : string ) : ResolvedProduct | undefined { const hit = resolved . get ( barcode ) ?? resolved . get ( normalizeBarcode ( barcode ) ) ; if ( ! hit || hit .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.