filesize

A lightweight, high-performance file size utility that converts bytes to human-readable strings. Zero dependencies. 100% test coverage.
Why filesize?
- Zero dependencies - Pure JavaScript, no external packages
- 100% test coverage - Reliable, well-tested codebase
- TypeScript ready - Full type definitions included
- Multiple standards - SI, IEC, and JEDEC support
- Localization - Intl API for international formatting
- BigInt support - Handle extremely large file sizes
- Functional API - Partial application for reusable formatters
- Browser & Node.js - Works everywhere
Installation
npm install filesize
TypeScript
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' });
Usage
import {filesize, partial} from "filesize";
filesize(1024);
filesize(265318);
filesize(1024, {standard: "iec"});
filesize(1024, {bits: true});
Partial Application
import {partial} from "filesize";
const formatBinary = partial({standard: "iec"});
formatBinary(1024);
formatBinary(1048576);
Options
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 |
Output Formats
filesize(1536);
filesize(1536, {output: "array"});
filesize(1536, {output: "object"});
filesize(1536, {output: "exponent"});
Standards
filesize(1000);
filesize(1024, {base: 2, standard: "iec"});
filesize(1024, {standard: "jedec"});
Examples
filesize(1024, {bits: true});
filesize(1024, {bits: true, base: 2});
filesize(1024, {fullform: true});
filesize(1024, {base: 2, fullform: true});
filesize(265318, {separator: ","});
filesize(1536, {round: 3, pad: true});
filesize(1536, {precision: 3});
filesize(265318, {locale: "de"});
filesize(1, {symbols: {B: "Π"}});
filesize(BigInt(1024));
filesize(-1024);
Error Handling
try {
filesize("invalid");
} catch (error) {
}
try {
filesize(1024, {roundingMethod: "invalid"});
} catch (error) {
}
Testing
npm test
npm run test:watch
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 |
--------------|---------|----------|---------|---------|-------------------
Development
npm install
npm run dev
npm run build
npm run lint
npm run lint:fix
Project Structure
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
Performance
- Basic conversions: ~16-27M ops/sec
- With options: ~5-13M ops/sec
- Locale formatting: ~91K ops/sec (use sparingly)
Optimization tips:
- Cache
partial() formatters for reuse
- Avoid locale formatting in performance-critical code
- Use
object output for fastest structured data access
Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
Changelog
See CHANGELOG.md for a history of changes.
License
Copyright (c) 2026 Jason Mulligan
Licensed under the BSD-3 license.