`startsWith` is 3.9× faster than a `/^s1/` regex
For a plain prefix check, startsWith beats a regular expression. Hoisting the regex literal out of the loop still leaves a 3.9× gap.
Result
| Run | `/^s1/` regex | `String.startsWith` | Ratio |
|---|---|---|---|
| Run 1 | 3.821 | 0.944 | 4.05× |
| Run 2 | 3.325 | 0.862 | 3.86× |
| Run 3 | 3.075 | 0.836 | 3.68× |
Conditions
One prefix test per string, across 100,000 strings.
Where this number does not apply
Once the pattern gets complex — several alternatives, suffix matches, boundary rules — the regex becomes the better tool.
Reproduce
cd bench
node --expose-gc micro-fixed.mjs --out ../data/run.json --samples 21 Run it three times on the same machine; if the spread of median stays under 10%, you should land on the same conclusion. If your numbers disagree, tell us and we will retract.