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

fast-prime-gen

v2.0.0

Published

Generate sequential primes through a generator

Readme

🔢 Fast Prime Gen

Generate sequential prime numbers, lazily and forever.

npm types license


A tiny, dependency-free package that streams prime numbers on demand. Instead of precomputing a list, it yields the next prime using a postponed Sieve of Eratosthenes (based on Vladimir Agafonkin's work) - so memory grows with the primes you've actually seen, not a fixed upper bound.

  • 🪶 Zero dependencies
  • ♾️ Lazy & infinite - pull one prime or a million
  • 🧩 Standard generator - works with for…of, spread, destructuring
  • 🔒 Typed - ships .d.ts, with ESM and CommonJS builds

Install

npm install fast-prime-gen

Quick start

import { primes } from 'fast-prime-gen';

const gen = primes();

gen.next().value;  // 2
gen.next().value;  // 3
gen.take(3);       // [5, 7, 11]  → continues from where you left off

primes().skip(100).next().value; // 547  → fresh generator, jump ahead
const { primes } = require('fast-prime-gen');

console.log(primes().take(5)); // [2, 3, 5, 7, 11]

Because it's a plain generator, it drops into any iterable context:

for (const p of primes()) {
  if (p > 50) break;
  console.log(p); // 2, 3, 5, 7, … 47
}

API

primes(): PrimeGenerator

A lazy, never-ending generator of sequential primes, plus the chainable helpers below.

.skip(amount = 1)

Advances past amount primes and returns the same generator for chaining. 0 is a no-op.

primes().skip(100).next().value; // 547

⚠️ Throws RangeError if amount isn't a non-negative integer.

.take(amount): number[]

Collects the next amount primes into an array. Composes with .skip().

primes().take(5);         // [2, 3, 5, 7, 11]
primes().skip(4).take(3); // [11, 13, 17]

⚠️ Throws RangeError if amount isn't a non-negative integer.

primesBelow(limit): number[]

Every prime strictly below limit (exclusive bound).

primesBelow(20); // [2, 3, 5, 7, 11, 13, 17, 19]

⚠️ Throws TypeError for a non-number, or RangeError if limit > Number.MAX_SAFE_INTEGER.

Limits

Primes use JavaScript number arithmetic, exact only up to Number.MAX_SAFE_INTEGER (2⁵³ − 1). Past that point the generator throws a RangeError rather than silently emit wrong values. Larger primes would need a bigint variant (not currently provided).

Migrating from 1.x

| 1.x | 2.x | | --- | --- | | const PostponedSieve = require('fast-prime-gen') | const { primes } = require('fast-prime-gen') | | JavaScript source, no types | TypeScript source, ships .d.ts | | skip() silently ignored bad input | skip() throws on non-integer / negative | | skip() only | added take() and primesBelow() | | wrong values past 2⁵³ | throws past Number.MAX_SAFE_INTEGER |

Development

npm install
npm test          # Vitest suite
npm run bench     # benchmarks
npm run lint      # Biome lint + format check  (lint:fix to auto-fix)
npm run typecheck # tsc --noEmit
npm run build     # dist/ → ESM + CJS + .d.ts  (tsup)

License

MIT