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, high-performance file size utility that converts bytes to human-readable strings. Zero dependencies. 100% test coverage.
npm install filesize
Fully typed with TypeScript 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' });
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"
import {partial} from "filesize";
const formatBinary = partial({standard: "iec"});
formatBinary(1024); // "1 KiB"
formatBinary(1048576); // "1 MiB"
| 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) |
round | number | 2 | Decimal places to round |
locale | string|boolean | "" | Locale for formatting, true for 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 specific exponent (-1 for auto) |
roundingMethod | string | "round" | Math method (round, floor, ceil) |
precision | number | 0 | Significant digits (0 for auto) |
pad | boolean | false | Pad decimal places |
// String (default)
filesize(1536); // "1.54 kB"
// Array
filesize(1536, {output: "array"}); // [1.54, "kB"]
// Object
filesize(1536, {output: "object"});
// {value: 1.54, symbol: "kB", exponent: 1, unit: "kB"}
// Exponent
filesize(1536, {output: "exponent"}); // 1
// SI (default, base 10)
filesize(1000); // "1 kB"
// IEC (binary, requires base: 2)
filesize(1024, {base: 2, standard: "iec"}); // "1 KiB"
// JEDEC (binary calculation, traditional symbols)
filesize(1024, {standard: "jedec"}); // "1 KB"
// Bits
filesize(1024, {bits: true}); // "8.19 kbit"
filesize(1024, {bits: true, base: 2}); // "8 Kibit"
// Full form
filesize(1024, {fullform: true}); // "1.02 kilobytes"
filesize(1024, {base: 2, fullform: true}); // "1 kibibyte"
// Custom separator
filesize(265318, {separator: ","}); // "265,32 kB"
// Padding
filesize(1536, {round: 3, pad: true}); // "1.536 kB"
// Precision
filesize(1536, {precision: 3}); // "1.54 kB"
// Locale
filesize(265318, {locale: "de"}); // "265,32 kB"
// Custom symbols
filesize(1, {symbols: {B: "Š"}}); // "1 Š"
// BigInt support
filesize(BigInt(1024)); // "1.02 kB"
// Negative numbers
filesize(-1024); // "-1.02 kB"
try {
filesize("invalid");
} catch (error) {
// TypeError: "Invalid number"
}
try {
filesize(1024, {roundingMethod: "invalid"});
} catch (error) {
// TypeError: "Invalid rounding method"
}
npm test # Run all tests (lint + node:test)
npm run test:watch # Live test watching
100% test coverage with 149 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 # Development mode with live reload
npm run build # Build distributions
npm run lint # Check code style
npm run lint:fix # Auto-fix linting issues
filesize.js/
āāā src/
ā āāā filesize.js # Main implementation (285 lines)
ā āāā helpers.js # Helper functions (215 lines)
ā āāā constants.js # Constants (81 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.