Post

Had a fun design challenge while making the barcode scanner. 🧵

Sam Rose@samwho.dev

I’ve taken to vibe coding little apps for my kids and hosting them at kids.samwho.dev. They love it. They’re all PWAs so they can look like apps on your home screen if you want. My eldest has loved self-scan at Tesco recently so I made a lil’ barcode scanner app for him 😁

I found an openly licensed set of products (“nudger” if you wanna look it up) and it has like 100M+ products in it. Perfect. Problem is: I don’t want to make a backend for this project. It’s a moving part I don’t care for. I just want to serve this statically out of nginx.

The full dataset, even when stripped to the bare minimum fields, was a few hundred megabytes. A bit much to load over the wire.

GTINs (barcode numbers) are 14 digits long and fit comfortably into JavaScript’s int53. So I broke the data out into roughly evenly sized chunks ordered by their GTIN and numbered them 0 to n.

Then I made an index that is literally just a list of ints, each int representing the maximum GTIN in the chunk associated with its position in the index array.

So you load this index when the page first loads (it’s about 100kb compressed), scan a barcode, and then binary search the index to find the chunk you’ll find it in. Then you load /chunks/<index>.json from the server.

0 reposts3 likes

Doesn’t require an extra process to accept requests and reads from a database. It’s a little extra work to set up but less work over time.

Right?? One of those things that sparks a thousand ideas. I look forward to the results of this (hopefully successful) nerdsnipe 😂

No backend API, everything loads super fast, each chunk is only about 50-100kb. I could have gone a lot further and scanned all of the barcodes in the house that my kids might scan into the app and make a special preloaded chunk with those in but that was less fun 😁