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 🙏

© 2024 – Pkg Stats / Ryan Hefner

regexp-worker

v3.0.0

Published

Runs regular expressions on a background thread.

Downloads

1,904

Readme

Regular Expression Worker

Execute Regular Expression Matches on a Node Worker Thread.

Regular Expressions can suffer from Catastrophic Backtracking. A very simple expression like /(x+x+)+y/ can cause your JavaScript application to freeze. This library allows you to run these expressions on another thread. If they take to long to complete, they are terminated, protecting your application from locking up.

Installation

npm install regexp-worker

Basic Usage

In the example below:

  1. a new Worker thread is created
  2. the regular expression is executed on the thread
  3. the result is returned
  4. the thread is stopped

For the occasional request, this is the easiest way, but the Worker startup and shutdown is expensive.

Find the words in some text

import { execRegExpOnWorker } from 'regexp-worker'
//...
const response = await execRegExpOnWorker(/\b\w+/g, 'Good Morning')
console.log(response.matches.map(m => m[0]))

Result:

  console.log
    [ 'Good', 'Morning' ]

Find the word breaks in some text

import { execRegExpOnWorker } from 'regexp-worker'
//...
const response = await execRegExpOnWorker(/\b/g, 'Good Morning');
console.log(response.matches.map(m => m.index))

Result:

  console.log
    [ 0, 4, 5, 12 ]

Format of the response

interface ExecRegExpResult {
    elapsedTimeMs: number;
    matches: RegExpExecArray[];
}

Where RegExpExecArray is RegExp.prototype.exec() result.

Creating a RegExpWorker Instance

To reduce the cost of starting and stopping the Worker, it is possible to create a RegExpWorker instance. This instance allows you to make multiple requests using the same worker. The request are queued and handled one at a time. If a request takes too long, it is terminated and the promise is rejected with an ErrorCanceledRequest.

import { RegExpWorker } from 'regexp-worker'

// ...
const defaultTimeOutMs = 10
const worker = new RegExpWorker(defaultTimeOutMs);

// Find all words in some text
let words = await worker.execRegExp(/\b\w+/g, 'Lots of text ...')

// Find all numbers in some text
let numbers = await worker.execRegExp(/\b\d+/g, 'Lots of text ...')

// Find 3 letter word pairs
let moreTimeMs = 100
let numbers = await worker.execRegExp(/\b\w{3}\s+\w{3}/g, 'Lots of text ...', moreTimeMs)

// ...

// It is a good idea to dispose of the worker before shutdown.
// The worker thread will stop on its own if left idle for more than 200ms.
worker.dispose();

Handling Timeouts

If the request times out, the promise will be rejected with:

interface TimeoutError {
    message: string;
    elapsedTimeMs: number;
}