filesize vs humanize-bytes vs pretty-bytes
File Size Formatting Libraries
filesizehumanize-bytespretty-bytesSimilar Packages:

File Size Formatting Libraries

File size formatting libraries are essential tools in web development that help convert raw byte values into human-readable formats. These libraries provide various ways to represent file sizes, making it easier for users to understand the size of files or data being processed. They often support different units of measurement, such as bytes, kilobytes, megabytes, and gigabytes, and can format the output to include appropriate suffixes and decimal precision. By using these libraries, developers can enhance user experience by presenting file sizes in a more intuitive manner, which is crucial in applications dealing with file uploads, downloads, or storage management.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
filesize01,70554.3 kB0a month agoBSD-3-Clause
humanize-bytes03-010 years agoMIT
pretty-bytes01,29816 kB08 months agoMIT

Feature Comparison: filesize vs humanize-bytes vs pretty-bytes

Formatting Options

  • filesize:

    filesize offers extensive formatting options, allowing developers to specify the number of decimal places, choose custom suffixes, and format sizes in a variety of ways. This flexibility makes it suitable for applications that need precise control over how file sizes are displayed.

  • humanize-bytes:

    humanize-bytes provides a straightforward approach to formatting file sizes, focusing on simplicity. It automatically selects the most appropriate unit and formats the output without additional configuration, making it ideal for quick implementations.

  • pretty-bytes:

    pretty-bytes strikes a balance between simplicity and customization, allowing developers to specify the number of decimal places while still providing a clean output. It is designed to be user-friendly while offering enough options for common use cases.

Performance

  • filesize:

    filesize is optimized for performance, handling large numbers of conversions efficiently. Its design ensures that formatting operations are quick, making it suitable for applications that require frequent file size calculations without noticeable lag.

  • humanize-bytes:

    humanize-bytes is lightweight and performs well for basic conversions. Its minimalistic approach ensures that it runs efficiently, making it a good choice for applications where performance is a priority and overhead needs to be minimized.

  • pretty-bytes:

    pretty-bytes is also designed for performance, with a focus on providing fast conversions while maintaining readability. It is efficient for most use cases, though it may not be as optimized as filesize for extensive formatting options.

Ease of Use

  • filesize:

    filesize has a slightly steeper learning curve due to its extensive options, but once understood, it offers powerful capabilities for formatting file sizes. Its API is well-documented, making it easier for developers to implement complex formatting.

  • humanize-bytes:

    humanize-bytes is extremely easy to use, with a simple API that allows developers to convert bytes to human-readable formats with minimal code. This makes it ideal for quick implementations and for developers who prioritize simplicity.

  • pretty-bytes:

    pretty-bytes is user-friendly and straightforward, providing an easy-to-understand API that allows for quick formatting of byte values. It strikes a good balance between ease of use and functionality.

Customization

  • filesize:

    filesize allows for high levels of customization, enabling developers to define their own suffixes and formatting rules. This feature is particularly useful for applications that require specific formatting standards or branding.

  • humanize-bytes:

    humanize-bytes offers limited customization options, focusing instead on providing a quick and standard output. This makes it less flexible for unique formatting needs but ideal for general use cases.

  • pretty-bytes:

    pretty-bytes provides some customization options, such as specifying decimal precision, allowing developers to tailor the output to their needs while still keeping the implementation simple.

Community and Support

  • filesize:

    filesize has a strong community and is actively maintained, ensuring that developers can find support and resources easily. Its popularity means that it is well-tested and reliable for production use.

  • humanize-bytes:

    humanize-bytes has a smaller community but is still maintained. It is suitable for developers who prefer lightweight libraries and do not require extensive support.

  • pretty-bytes:

    pretty-bytes enjoys a good level of community support and is actively maintained, making it a reliable choice for developers looking for a balance between features and ease of use.

How to Choose: filesize vs humanize-bytes vs pretty-bytes

  • filesize:

    Choose filesize if you need a robust solution that offers extensive formatting options, including the ability to specify decimal places and custom suffixes. It is ideal for applications that require precise control over the output format.

  • humanize-bytes:

    Select humanize-bytes for a lightweight and straightforward solution that focuses on simplicity and ease of use. This package is suitable for projects where minimalism is key, and you want to quickly convert bytes to a human-readable format without additional features.

  • pretty-bytes:

    Opt for pretty-bytes if you prefer a package that provides a balance between simplicity and customization. It allows for easy formatting of byte values with options for decimal precision and is particularly useful for applications that require a clean and readable output.

README for filesize

filesize

npm version Node.js Version License Build Status

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); // "1.02 kB"
filesize(265318); // "265.32 kB"
filesize(1024, {standard: "iec"}); // "1 KiB"
filesize(1024, {bits: true}); // "8.19 kbit"

Partial Application

import {partial} from "filesize";

const formatBinary = partial({standard: "iec"});
formatBinary(1024); // "1 KiB"
formatBinary(1048576); // "1 MiB"

Options

OptionTypeDefaultDescription
bitsbooleanfalseCalculate bits instead of bytes
basenumber-1Number base (2 for binary, 10 for decimal, -1 for auto)
roundnumber2Decimal places to round
localestring|boolean""Locale for formatting, true for system locale
localeOptionsObject{}Additional locale options
separatorstring""Custom decimal separator
spacerstring" "Value-unit separator
symbolsObject{}Custom unit symbols
standardstring""Unit standard (si, iec, jedec)
outputstring"string"Output format (string, array, object, exponent)
fullformbooleanfalseUse full unit names
fullformsArray[]Custom full unit names
exponentnumber-1Force specific exponent (-1 for auto)
roundingMethodstring"round"Math method (round, floor, ceil)
precisionnumber0Significant digits (0 for auto)
padbooleanfalsePad decimal places

Output Formats

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

Standards

// 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"

Examples

// 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"

Error Handling

try {
  filesize("invalid");
} catch (error) {
  // TypeError: "Invalid number"
}

try {
  filesize(1024, {roundingMethod: "invalid"});
} catch (error) {
  // TypeError: "Invalid rounding method"
}

Testing

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

Development

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

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:

  1. Cache partial() formatters for reuse
  2. Avoid locale formatting in performance-critical code
  3. 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.