npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

stable-regex

v1.0.1

Published

Make every regex check deterministic by resetting lastIndex before test().

Readme

stable-regex

Fast, deterministic regex checks for hot paths.

Last version Coverage Status NPM Status

Why this package exists

When a regex has g or y, .test() mutates lastIndex.

const pattern = /hello/g

pattern.test('hello') // true
pattern.test('hello') // false
pattern.test('hello') // true

In hot code paths, developers usually pick one of three options:

  • pattern.test(input) (fast but wrong for reused stateful regexes)
  • new RegExp(...).test(input) each call (correct but allocates and is slower)
  • manual pattern.lastIndex = 0 before test (correct but repetitive boilerplate)

The stable-regex library resets the index just when needed, keeping the performance stable and predictable.primitive.

Use stable-regex when you:

  • Reuse precompiled regexes in request handlers/parsers/normalizers.
  • Need deterministic boolean checks with g/y patterns.
  • Want high throughput without repeating reset boilerplate.

This library is 2.6x faster than recreating a regex each call, which is the solution most used.

Install

npm install stable-regex

Usage

const stableRegex = require('stable-regex')

const pattern = /hello/g

stableRegex(pattern, 'hello') // true
stableRegex(pattern, 'hello') // true
stableRegex(pattern, 'hello') // true

API

stableRegex(pattern, input)

pattern

Required Type: RegExp or RegExp-like object exposing .test()

If lastIndex is numeric, it is reset before testing.

input

Required Type: string

Returns boolean.

Throws TypeError when pattern does not expose .test().

Benchmark

Naive alternatives

Node v25.6.1 on Apple Silicon, 12,000,000 iterations:

| Case | Ops/sec | ns/op | Correct for reused /.../g | | --- | ---: | ---: | :---: | | pattern.test(input) | 78,175,641 | 12.79 | ❌ | | new RegExp(...).test(input) each call | 18,652,815 | 53.61 | ✅ | | manual lastIndex = 0; test() | 48,754,401 | 20.51 | ✅ | | stableRegex(pattern, input) | 48,406,721 | 20.66 | ✅ |

Real-world numbers

From the @unavatar/core hot path (src/avatar/auto.js) comparing:

  • stableRegex(precompiledPattern, input)
  • dataUriRegex().test(input) (fresh regex each call)

Setup: Node v25.6.1, Apple Silicon, 10,000,000 iterations, 7 rounds, median.

| Case | stable-regex (ms) | fresh regex each call (ms) | Speedup | | --- | ---: | ---: | ---: | | Data URI match (data:image/...) | 288.50 | 976.70 | 3.39x | | Non-match (https://example.com/...) | 67.07 | 730.39 | 10.89x |

That translates to:

  • 70.46% less time on matching inputs.
  • 90.82% less time on non-matching inputs.

License

stable-regex © Kiko Beats, released under the MIT License.

Maintained by Kiko Beats with help from contributors.

kikobeats.com · GitHub Kiko Beats · Twitter @kikobeats