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-balance

v1.0.5

Published

Blazing fast chemical equation balancer - handles complex formulas, redox, charges, hydrates, all arrow styles.

Readme

fast-balance

Blazing fast chemical equation balancer for JavaScript and TypeScript
Handles complex formulas, redox, charges, hydrates, and all common arrow styles – with zero floating‑point errors.

npm version license TypeScript

Features

  • Simple to complex – balances anything from H2 + O2 -> H2O to multi‑species redox reactions.
  • Nested groups – parentheses () and brackets [] with subscripts, e.g. Ca3(PO4)2, [Fe(CN)6]4-.
  • Ions & chargesFe2+, SO4^2-, MnO4-, etc.
  • Electronse- or e for redox half‑reactions.
  • HydratesCuSO4·5H2O (dot, middle dot, bullet, asterisk).
  • State symbols(s), (l), (g), (aq), (solid), (liquid), (gas), (aqueous), (cr), (am) are automatically stripped.
  • Arrow styles->, , , , <=>, <->, -->, =.
  • Exact integer arithmetic – uses rational numbers under the hood, so you always get the smallest whole‑number coefficients with no rounding errors.
  • Parses input – leading coefficients (like 2 H2 + O2) are ignored so you can feed in an already‑balanced equation without breaking it.
  • Tiny & fast – ~6 kB minified, no dependencies.

Installation

npm install fast-balance

fast-balance is ESM‑only. It works with Node.js ≥12.20, modern bundlers, and browsers that support ES modules.

Quick start

import { balance } from 'fast-balance';

const result = balance('H2 + O2 -> H2O');
console.log(result.equation); // "2 H2 + O2 -> 2 H2O"

With options

// Hide "1" coefficients, output as HTML
const result = balance('Fe2+ + Cl- -> FeCl2', { showOne: false, format: 'html' });
console.log(result.equation); // "Fe2+ + 2 Cl- &rarr; FeCl2"

API

balance(input: string, options?: BalanceOptions): BalanceResult

Balances a chemical equation string.

input (string)

The equation string. Any spaces and leading coefficients are allowed.
Supported arrow symbols: ->, , , , <=>, <->, -->, =.

options (object, optional)

| Option | Type | Default | Description | |----------|--------------------------|----------|---------------------------------------------------------| | showOne| boolean | true | Whether to show the coefficient 1 (e.g. "1 H2O") | | format | "text" \| "html" \| "latex" | "text" | Output style for the equation string |

BalanceResult

{
  reactants: Array<{ coefficient: number; formula: string }>;
  products:  Array<{ coefficient: number; formula: string }>;
  equation:  string;   // formatted according to `options.format`
}

Supported notation

| Feature | Examples | |-----------------------------|-------------------------------------------------| | Elements | H, He, C, O, Fe, Uut | | Subscripts | H2O, C6H12O6 | | Parentheses / brackets | Ca3(PO4)2, [Fe(CN)6]4-, Al2(SO4)3 | | Ionic charges | Fe2+, Fe3+, SO4^2-, MnO4-, O^2- | | Electrons | e- or e (also e+) | | Hydrate dots | CuSO4·5H2O, CuSO4*5H2O, CuSO4•5H2O | | State symbols (ignored) | H2O(l), CO2(g), NaCl(aq), AgCl(s) | | Arrow variants | ->, , , , <=>, <->, -->, = | | Leading coefficients (ign.) | 2 H2 + O2 -> 2 H2O is parsed correctly |

How it works

  1. Parse each chemical term into an element‑count map and a net charge.
  2. Build a linear system based on the conservation of each element and charge.
  3. Solve for the nullspace using exact rational (fraction) arithmetic – no floating‑point drift.
  4. Convert the rational solution to the smallest possible integer coefficients.

If the equation has multiple degrees of freedom (e.g. a reaction that can be scaled arbitrarily), the algorithm picks the minimal integer solution.

TypeScript

Full type definitions are included (dist/index.d.ts). You get auto‑completion for BalanceOptions, BalanceResult, etc.

License

MIT © rjiang880
See the LICENSE file for details.