Why Slug Generation Gets Slower the More Titles You Feed It
I benchmarked two ways to turn a title into a URL slug, across 1,000 to 20,000 titles A single chained-regex version took 62.77ms at 20,000 titles, a manual character-loop version took 32.74ms on the same input Below 1,0
I benchmarked two ways to turn a title into a URL slug, across 1,000 to 20,000 titles
A single chained-regex version took 62.77ms at 20,000 titles, a manual character-loop version took 32.74ms on the same input
Below 1,000 titles the two were close enough to call a tie, roughly 7ms versus 6.4ms
Both produced identical slugs on every title I tested, including ones with accents, so correctness was never the question
Every product, every blog post, every page on a Shopify store eventually needs a slug, and almost nobody times the function that makes one. I had two versions sitting in different projects, a regex-chain and a manual character loop, and no real idea which one actually cost more once the volume went up.
The Two Ways I Actually Had Written This
The regex version is the one that shows up in most tutorials, chained through a handful of replace calls: lowercase the string, normalize it so accented characters split into a base letter and a separate combining mark, strip those combining marks out, collapse anything that isn't a letter or digit into a single dash, then trim leading and trailing dashes. Four regex passes over the string, each one doing its own full scan.
The manual version walks the string once, character by character, checking each code point directly against the ranges for lowercase letters and digits, skipping combining diacritic marks by their Unicode range, and inserting a single dash whenever it hits a run of characters that don't qualify, without ever allowing two dashes in a row.
Both run toLowerCase() and normalize("NFKD") first. That part is identical. Where they diverge is everything after: four chained regex replaces against one pass that makes its decision character by character as it goes.
I'd always assumed the regex version was fine because it's shorter, and shorter usually reads as simpler. Reading simplicity and runtime cost aren't the same thing, which is exactly the assumption I wanted to test.
Four lines against one loop, doing the same job on paper.
What I Actually Measured
I generated batches of realistic blog-style titles, several variants cycled with an incrementing suffix so no two were identical, and ran both functions against the same batches, same process, same machine, timed with process.hrtime.bigint() around each full batch.
At 1,000 titles, the regex version took 7.12ms. The manual loop took 6.40ms. Close enough that I would have called it a rounding error if I'd only run this one size and stopped there.
That's the trap with any single-size benchmark. One data point tells you nothing about a trend.
At 5,000 titles, the regex version took 16.36ms. The manual loop took 9.15ms, already closer to half. That's the first point where the two stop looking like the same function wearing different syntax.
At 10,000 titles, regex: 42.58ms. Manual loop: 17.08ms. At 20,000 titles, regex: 62.77ms. Manual loop: 32.74ms. The manual version stayed close to twice as fast across the higher end of the range I tested, consistently, not as a one-off blip on a single run.
Neither number is slow in isolation. 62.77ms for 20,000 titles is nothing if you run it once, during a migration or a bulk import. It only turns into a cost that compounds if a slug function sits somewhere that runs often, on every save of every product or post, where the per-call overhead multiplies by how many times a day the path gets hit, not by how many titles exist in total.
Context decides whether 63 milliseconds is a number worth caring about or a number nobody will ever see.
Why Four Passes Cost More Than One
This isn't really about regex being slow in general. A single, well-written regex over a string is usually fast. The cost here comes from chaining four of them, each one a full linear scan over the string on its own, one after another, each producing a new intermediate string for the next pass to scan again.
The manual version pays for one scan total. It reads each character exactly once, decides immediately whether to keep it, skip it, or replace it with a dash, and writes the result as it goes. No intermediate strings get built and thrown away between steps, because there's only one step.
Four passes over a short title barely register. Four passes over a longer title, or four passes repeated across a batch import, add up in a way a single pass structurally can't, which is the same shape of cost I ran into benchmarking three ways to deduplicate an array: the interface looks the same, one function call, one string in, one string out, while the actual work happening underneath scales completely differently depending on which approach you picked.
The accent handling is where I expected the manual version to lose ground, and it didn't. Both functions ran normalize("NFKD") upfront, which does the hard part of splitting an accented character into its base letter plus a separate combining mark. After that, the manual loop just has to recognize and skip a known Unicode range for combining marks, which is one more check per character, not a second full pass.
Is the Difference Worth the Less Readable Code?
Here's the honest tradeoff, not the one that makes the faster version look obviously correct. The regex version is four readable lines. Anyone can open it, read what each replace does, and understand the whole function in under a minute. The manual version is a loop with character-code comparisons, and it takes real concentration to verify it's actually correct, especially the part that has to recognize combining marks by number range instead of by name.
I ran both against the same batch of titles, including ones with accented characters, and compared every output. They matched, every single time, on every title I tested. So correctness wasn't the deciding factor here, speed was, and that's a genuinely different question from which one I'd want to hand to someone reading this code for the first time.
For a function that runs once per save, on a single title, at human typing speed, I would keep the regex version without a second thought. Readability wins outright when the cost difference between two options is a fraction of a millisecond that nobody will ever notice.
For a bulk import or a migration script touching thousands of titles in one run, I'd reach for the manual version, write one clear comment explaining what the character ranges are doing, and accept the readability cost in exchange for a script that finishes in half the time. The decision isn't about which function is better. It's about which cost, slower code or harder-to-read code, actually matters for the one place you're about to use it.
There's a middle option I didn't test here but would consider before reaching for the full character loop: keep the regex version, just collapse it from four separate replace calls into fewer passes by combining character classes into a single pattern where the logic allows it. That doesn't close the whole gap, a lot of the cost is in how many times the string gets rescanned, not in which exact characters each pass is looking for, but it's a smaller change than rewriting the function by hand, and it's worth trying first if a profiler points at this exact spot and a full rewrite feels like overkill for the size of the win.
I didn't test that middle version here. I'm naming it because the honest answer to "which slug function should I use" is rarely just one of two extremes, and pretending otherwise would be tidier than the real decision usually is.
Bottom Line
Two slug functions, same output on every input I threw at them, a genuine and repeatable speed gap that only shows up once volume climbs past a few thousand items. Below that, it's noise, and I wouldn't touch working code to chase a number nobody will ever see move.
I'm keeping both versions, on purpose, in different places. The regex chain stays wherever a single title gets slugged on save, because four readable lines beat a faster function nobody needs there. The manual loop is what I reach for the moment a script has to process a batch, because that's exactly the situation where a function gets called enough times for the gap to stop being theoretical. Measuring both took one afternoon. Guessing wrong and shipping the slow one into a hot path would have cost more than that, just later, and quietly enough that I might never have traced it back to a slug function at all.
This article contains affiliate links. If you sign up through them, I may earn a small commission at no extra cost to you. (Ad)
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.