pretty-bytes

Convert byte counts into human-readable strings like 1337 → 1.34 kB, with binary, bit, and locale-aware formatting options.

Library
npm
v7.2.0
1,319 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
60 /100 Good
Development Activity 56
Maintenance 44
Community 52
Maturity 60
Momentum 28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
71 /100 Good
Architecture 78
Code Quality 92
Innovation 80
Learning Curve 35

pretty-bytes is a zero-dependency utility that turns raw byte counts into strings people can actually read, converting 1337 into 1.34 kB using SI decimal units by default. It handles both number and bigint inputs, so it works equally well for small UI values and for arbitrarily large byte counts that would otherwise lose precision in a Number.

Beyond the basic conversion, it exposes a focused set of options for real-world display needs: binary (KiB/MiB) units for memory reporting, bit units for bitrate displays, signed output for diffs, locale-aware number formatting, explicit fraction-digit control, and fixed-width padding for aligning values in tables or progress bars. The whole implementation ships as a single small ESM module with a hand-written TypeScript declaration file, making it a common building block wherever a byte count needs to be shown to a user.

What You Get

  • A single prettyBytes(number, options?) function with no runtime dependencies, distributed as ESM with bundled TypeScript types.
  • Support for both number and bigint inputs, so extremely large byte counts (beyond safe integer range) still format correctly.
  • SI (kB, MB, GB…) and binary (KiB, MiB, GiB…) unit modes, plus a parallel bit/bibit mode for bitrate displays.
  • Locale-aware output via Intl-backed toLocaleString, with explicit minimum/maximum fraction-digit control and truncation instead of rounding when digits are specified.
  • A fixedWidth option that right-pads results for aligned columns in tables, logs, or progress bars.

Common Use Cases

  • Displaying file sizes in upload/download UIs, file managers, or admin dashboards.
  • Formatting memory, disk, or bandwidth usage in CLI tools and monitoring dashboards.
  • Showing bitrate or transfer-speed figures in networking or media tooling.
  • Rendering aligned, fixed-width byte columns in terminal tables or progress bars.

Under The Hood

Architecture

The whole package is a single ESM module (index.js) built from small, composable pure functions rather than a class or stateful pipeline: toLocaleString handles locale-aware number rendering, log10/log/divide implement bigint-safe math (since Math.log/division don’t work directly on bigint), buildLocaleOptions translates the public options into Intl number-format options, and applyFixedWidth handles the final padding step. The exported prettyBytes function composes these in a clear linear flow — validate input, normalize options, pick a unit table, compute the exponent, divide, format, then pad — so despite having no internal module boundaries, the separation of concerns is explicit and each helper could be swapped or extended (e.g. new unit tables) without touching the others.

Tech Stack

The package has zero runtime dependencies and ships as pure ESM ("type": "module") targeting Node.js 20+, relying only on built-in Number#toLocaleString/Intl for localization. Its devDependencies are entirely tooling: ava for the test runner, xo (an opinionated ESLint preset used across Sindre Sorhus’s packages) for linting, and tsd for type-level testing against the hand-written index.d.ts. There is no build step — the published package is the same JavaScript and declaration file present in the repo.

Code Quality

The test suite (test.js) is extensive relative to the package’s size, covering invalid-input errors, bigint vs number parity, rounding behavior at every unit boundary, binary vs SI units, locale formatting, signed output, and fixed-width padding edge cases. Type safety is enforced two ways: a hand-authored index.d.ts with JSDoc-documented options, and index.test-d.ts running tsd type-level assertions against it. Error handling is explicit — invalid numeric input and invalid fixedWidth values throw descriptive TypeErrors rather than failing silently. GitHub Actions CI (.github/workflows) runs the xo && ava && tsd script on each change.

API Design The public surface is a single function with sensible defaults (unlabeled call prettyBytes(1337) just works), and every non-default behavior is opt-in through a flat options object — signed, bits, binary, locale, minimumFractionDigits, maximumFractionDigits, space, nonBreakingSpace, fixedWidth. This keeps the common case a one-line call while still covering less common needs like bitrate display, table alignment, and internationalization without requiring a second API or configuration object. Documentation in the README and type declarations pairs every option with a runnable example, which keeps the learning curve low despite the fairly broad option surface.

Used by 14 apps in this directory

TypeScript
92%
Other

n8n

Automation · No Code Platforms

208,437

Code when you need it, UI when you don't — the workflow automation platform built for technical teams who refuse to choose.

View details
95
Repo Health
87
Technical
62
Dependency
Built with
TypeScript 92%
Updated 6 days ago
Python
55%
Other

PostHog

Ab Testing Experimentation · Analytics · Developer Tools

40,397

The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.

View details
92
Repo Health
80
Technical
65
Dependency
Built with
Python 55%
TypeScript 36%
Updated 5 days ago
Python
100%
Other

Sourcebot

AI Code Assistants · Developer Tools · Search

4,005

A self-hosted, AI-powered code search engine that indexes every repo across GitHub, GitLab, Bitbucket, Gitea, Gerrit, and Azure DevOps, so both engineers and coding agents can search, browse, and ask questions about your codebase from one place.

View details
86
Repo Health
83
Technical
61
Dependency
Built with
Python 100%
Updated 1 weeks ago
Rust
93%
Other

Tabby

AI Code Assistants

33,924

Self-hosted AI coding assistant — run GitHub Copilot-grade code completion on your own hardware with no cloud dependency.

View details
55
Repo Health
78
Technical
61
Dependency
Built with
Rust 93%
Updated 4 months ago
TypeScript
99%
AGPL 3.0

Webstudio

Design Tools · No Code Platforms

9,103

Open source visual development platform with complete CSS control, headless CMS integration, and full infrastructure ownership

View details
91
Repo Health
79
Technical
60
Dependency
Built with
TypeScript 99%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers