The filesize, humanize-bytes, and pretty-bytes libraries are specialized utilities designed to convert raw byte integers into human-readable strings (e.g., turning 1024 into 1 KB). While they share a common goal, they differ significantly in flexibility, configuration depth, and output style. filesize offers the most granular control over formatting, bases, and rounding, making it suitable for complex data tables or technical dashboards. pretty-bytes focuses on concise, opinionated defaults ideal for general UI elements like file lists or download progress. humanize-bytes provides a straightforward, lightweight alternative but requires careful evaluation regarding its current maintenance status compared to the other two.
When displaying storage capacity, file sizes, or network transfer rates to users, raw bytes (like 1536000) are meaningless. Developers need to convert these numbers into readable formats like "1.5 MB". While this seems simple, differences in rounding, base calculation (binary vs. decimal), and localization can lead to inconsistent user experiences. The filesize, humanize-bytes, and pretty-bytes packages solve this problem but take different approaches to flexibility and defaults.
filesize is built for power users who need exact control over every aspect of the output string. It allows you to define the number of decimal places, the base (binary or decimal), the output format (string, object, or array), and even custom spacer characters.
import { filesize } from 'filesize';
// Default usage
filesize(1024);
// Output: "1 kB"
// Custom configuration: 2 decimal places, binary base (1024), no spacer
filesize(1024, {
base: 2,
round: 2,
spacer: ""
});
// Output: "1.00KiB"
// Returning an object for custom rendering
filesize(1024, { output: "object" });
// Output: { value: 1, symbol: "kB", ... }
pretty-bytes takes an opinionated approach. It aims to provide the most readable string with minimal configuration. You can specify the number of significant digits or force a specific unit, but it generally discourages deep customization to maintain consistency.
import prettyBytes from 'pretty-bytes';
// Default usage
prettyBytes(1024);
// Output: "1 kB"
// Limiting significant digits
prettyBytes(1025, { maximumSignificantDigits: 3 });
// Output: "1.03 kB"
// Forcing a specific unit
prettyBytes(1024, { unit: 'megabyte' });
// Output: "0.001 MB"
humanize-bytes offers a simpler API with fewer configuration options. It typically accepts the number and an optional options object for precision, focusing on getting a standard string quickly without the extensive feature set of filesize.
import humanizeBytes from 'humanize-bytes';
// Default usage
humanizeBytes(1024);
// Output: "1 KB"
// With precision option
humanizeBytes(1024, { precision: 2 });
// Output: "1.00 KB"
A common source of bugs in file formatting is mixing up binary (KiB, MiB) and decimal (KB, MB) units. Operating systems often disagree on which to use.
filesize lets you explicitly toggle this behavior. By default, it often uses decimal (SI) standards, but you can switch to binary (IEC) easily.
import { filesize } from 'filesize';
const size = 1048576; // 1024 * 1024
// Decimal (SI) - Default in many configs
filesize(size, { base: 10 });
// Output: "1.05 MB"
// Binary (IEC)
filesize(size, { base: 2 });
// Output: "1 MiB"
pretty-bytes also supports this distinction but exposes it through the binary option. This is crucial when matching OS-specific reporting (e.g., macOS uses binary, while some network tools use decimal).
import prettyBytes from 'pretty-bytes';
const size = 1048576;
// Decimal (Default)
prettyBytes(size);
// Output: "1.05 MB"
// Binary
prettyBytes(size, { binary: true });
// Output: "1 MiB"
humanize-bytes generally defaults to one standard (often binary or decimal depending on the version and implementation details) and may offer less explicit control over switching bases compared to the other two. Developers must verify the default behavior in their specific version to ensure it matches their application's requirements.
import humanizeBytes from 'humanize-bytes';
// Behavior depends on library version defaults
// Often requires checking docs to confirm if 1024 or 1000 is used for 'KB'
humanizeBytes(1048576);
// Output varies: "1 MB" or "1.05 MB" depending on base logic
How units are labeled matters for international audiences. Some libraries use "KB", others "kB", and some support full words like "Kilobytes".
filesize has robust support for custom symbols and locales. You can pass a locale string to format numbers according to regional standards (e.g., using commas vs. periods for decimals) and even override unit symbols.
import { filesize } from 'filesize';
// Using German locale for number formatting
filesize(1024.5, { locale: 'de-DE' });
// Output: "1,02 kB" (Note the comma)
// Custom symbols
filesize(1024, { symbols: { kb: "Ko" } }); // French convention example
// Output: "1 Ko"
pretty-bytes handles localization primarily through the locale option, which formats the number part of the string. It sticks to standard unit abbreviations but ensures the numeric portion respects the user's region.
import prettyBytes from 'pretty-bytes';
// French locale
prettyBytes(1024.5, { locale: 'fr-FR' });
// Output: "1,03 kB"
humanize-bytes typically provides basic number formatting but may lack the deep locale integration found in filesize. It is best suited for applications where default English formatting is acceptable or where custom post-processing is planned.
import humanizeBytes from 'humanize-bytes';
// Basic usage, locale support may be limited or absent depending on version
humanizeBytes(1024.5);
// Output: "1.02 KB"
Before adding any dependency, checking its maintenance status is critical.
filesize: Actively maintained. It is a mature library with regular updates, strong TypeScript support, and a large user base. It is safe for long-term projects.pretty-bytes: Actively maintained by a prominent developer in the JavaScript ecosystem. It is stable, well-tested, and widely used in modern tooling.humanize-bytes: Caution advised. This package has seen significantly less activity compared to the others. In some contexts, similar packages have been deprecated in favor of more robust alternatives. Always check the latest npm page and repository for deprecation notices before selecting this for a new architecture. If the repository is archived or has no recent commits, prefer filesize or pretty-bytes.| Feature | filesize | pretty-bytes | humanize-bytes |
|---|---|---|---|
| Primary Goal | Maximum flexibility & control | Sensible defaults & simplicity | Basic conversion |
| Base Control | Explicit (base: 2 or 10) | Explicit (binary: true) | Limited / Implicit |
| Output Formats | String, Object, Array, Exponent | String only | String only |
| Localization | Advanced (Locale + Custom Symbols) | Standard (Locale) | Basic / None |
| Maintenance | β Active | β Active | β οΈ Verify Status |
For complex enterprise applications where you need to match specific design systems, support multiple locales deeply, or display data in non-standard ways (like returning objects for custom rendering), filesize is the superior choice. Its API is verbose but powerful.
For standard web applications, dashboards, and UI components where you just need a clean, readable string without spending time configuring options, pretty-bytes is the ideal default. It balances size, features, and ease of use perfectly.
Avoid humanize-bytes for new projects unless you have a specific legacy constraint, as the other two options offer better long-term stability, clearer documentation, and more active community support. Always prioritize libraries that are actively maintained to ensure security and compatibility with future JavaScript standards.
Choose filesize when you need precise control over output formatting, such as enforcing specific decimal places, switching between binary (1024) and decimal (1000) bases, or customizing spacer characters. It is the best fit for enterprise dashboards, data-heavy tables, or scenarios where consistency across different locales and units is critical.
Choose humanize-bytes only if you are maintaining a legacy project that already depends on it or if you require a very specific, simple output format that matches its defaults exactly. For new projects, proceed with caution and verify its current maintenance status, as more robust and actively updated alternatives exist in the ecosystem.
Choose pretty-bytes for modern web applications where you need a reliable, zero-config solution that looks good out of the box. It is ideal for file uploaders, disk space indicators, and general UI components where standard formatting (like '1.5 MB') is preferred over highly customized technical strings.
A lightweight, zero-dependency JavaScript utility that converts bytes to human-readable strings. Built for client and server applications that need to display file sizes β from download counters to disk-usage reports.
Number.MAX_SAFE_INTEGER.partial() creates reusable, immutable formatters.npm install filesize
import {filesize, partial} from "filesize";
filesize(1024); // "1.02 kB"
filesize(265318); // "265.32 kB"
filesize(1024, {standard: "iec"}); // "1 KiB"
filesize(1024, {bits: true}); // "8.19 kbit"
partial() returns a pre-configured formatter with frozen options. Use it when you format many values with the same settings β it avoids re-parsing options on every call.
import {partial} from "filesize";
const formatBinary = partial({standard: "iec"});
formatBinary(1024); // "1 KiB"
formatBinary(1048576); // "1 MiB"
filesize supports three unit standards. They differ in two ways: the base (1000 or 1024) and the unit symbols.
| Standard | Base | Unit symbols | Example |
|---|---|---|---|
| SI | 1000 | kB, MB, GB | filesize(1000) β "1 kB" |
| IEC | 1024 | KiB, MiB, GiB | filesize(1024, {standard: "iec"}) β "1 KiB" |
| JEDEC | 1024 | KB, MB, GB | filesize(1024, {standard: "jedec"}) β "1 KB" |
When you set standard, the base is implied and base is ignored. base is only consulted when standard is not set.
| Option | Type | Default | Description |
|---|---|---|---|
bits | boolean | false | Calculate bits instead of bytes |
base | number | -1 | Number base (2 for binary, 10 for decimal, -1 for auto). Ignored when standard is set |
round | number | 2 | Decimal places to round to |
precision | number | 0 | Significant digits (0 for auto). When set, overrides round |
pad | boolean | false | Pad decimal places to match round |
locale | string|boolean | "" | Locale for formatting; true for the system locale |
localeOptions | Object | {} | Additional locale options |
separator | string | "" | Custom decimal separator |
spacer | string | " " | Value-unit separator |
symbols | Object | {} | Custom unit symbols |
standard | string | "" | Unit standard (si, iec, jedec) |
output | string | "string" | Output format (string, array, object, exponent) |
fullform | boolean | false | Use full unit names |
fullforms | Array | [] | Custom full unit names |
exponent | number | -1 | Force a specific exponent (-1 for auto) |
roundingMethod | string | "round" | Math method (round, floor, ceil) |
round controls decimal places; precision controls significant digits. When precision is greater than 0, it takes precedence over round.
// String (default)
filesize(1536); // "1.54 kB"
// Array: [value, symbol]
filesize(1536, {output: "array"}); // [1.54, "kB"]
// Object: {value, symbol, exponent, unit}
filesize(1536, {output: "object"});
// {value: 1.54, symbol: "kB", exponent: 1, unit: "kB"}
// Exponent: the unit index
filesize(1536, {output: "exponent"}); // 1
// Bits
filesize(1024, {bits: true}); // "8.19 kbit"
filesize(1024, {bits: true, base: 2}); // "8 Kibit"
// Full unit names
filesize(1024, {fullform: true}); // "1.02 kilobytes"
filesize(1024, {base: 2, fullform: true}); // "1 kibibyte"
// Custom decimal separator
filesize(265318, {separator: ","}); // "265,32 kB"
// Padding
filesize(1536, {round: 3, pad: true}); // "1.536 kB"
// Significant digits
filesize(1536, {precision: 3}); // "1.54 kB"
// Locale
filesize(265318, {locale: "de"}); // "265,32 kB"
// Custom symbols
filesize(1, {symbols: {B: "Π"}}); // "1 Π"
// BigInt
filesize(BigInt(1024)); // "1.02 kB"
// Negative numbers
filesize(-1024); // "-1.02 kB"
filesize() throws a TypeError for invalid input.
try {
filesize("invalid");
} catch (error) {
// TypeError: "Invalid number"
}
try {
filesize(1024, {roundingMethod: "invalid"});
} catch (error) {
// TypeError: "Invalid rounding method"
}
Invalid input includes non-numeric values, NaN, Infinity, and BigInt values that overflow Number.MAX_SAFE_INTEGER.
Fully typed with definitions included:
import {filesize, partial} from "filesize";
const result: string = filesize(1024);
const formatted: {value: number; symbol: string; exponent: number; unit: string} = filesize(1024, {output: "object"});
const formatter: (arg: number | bigint) => string = partial({standard: "iec"});
npm test # Run all tests (lint + node:test)
npm run test:watch # Live test watching
100% test coverage with 255 tests:
--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
constants.js | 100 | 100 | 100 | 100 |
filesize.js | 100 | 100 | 100 | 100 |
helpers.js | 100 | 100 | 100 | 100 |
--------------|---------|----------|---------|---------|-------------------
npm install # Install dependencies
npm run dev # Build distributions in watch mode
npm run build # Build distributions
npm run lint # Check code style
npm run fix # Auto-fix linting issues
filesize.js/
βββ src/
β βββ filesize.js # Main implementation (286 lines)
β βββ helpers.js # Helper functions (538 lines)
β βββ constants.js # Constants (82 lines)
βββ tests/
β βββ unit/
βββ dist/ # Built distributions
βββ types/ # TypeScript definitions
Optimization tips:
partial() formatters for reuseobject output for fastest structured data accessWe welcome contributions! Please see our Contributing Guidelines for details.
See CHANGELOG.md for a history of changes.
Copyright (c) 2026 Jason Mulligan
Licensed under the BSD-3 license.