Skip to content

rabbxdev/ms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@rabbx/ms

Ultrafast formatter and parser for time durations. 20-30x faster than the original ms package, zero dependencies, 450 bytes gzipped.

Works with nanoseconds to days. Perfect for logging, benchmarks, and timers.

Install

npm i @rabbx/ms

Usage

import ms, { parse, msCompact } from '@rabbx/ms';

// Format durations
ms(1500)                 // "1.5µs"
ms(1_500_000)            // "1.50ms"
ms(90_000_000_000)       // "1m 30s"
ms(3_720_000_000_000)    // "1h 2m"
ms(-90_000_000_000)      // "-1m 30s"

// Compact format for logs/metrics
msCompact(90_000_000_000) // "1.50m"

// Parse strings back to nanoseconds
parse('1m 30s')          // 90000000
parse('2.5h')            // 9000000
parse('500ms')           // 500000
parse('-1m 30s')         // -90000000

API

ms(ns: number): string

Format a duration in nanoseconds to a human-readable string.
Uses 2 decimals for seconds/minutes, 1 decimal for µs, 0 for larger units.

Units used:

  • ns - nanoseconds
  • µs - microseconds
  • ms - milliseconds
  • s - seconds
  • m - minutes
  • h - hours
  • d - days

msCompact(ns: number): string

Same as ms() but always returns a single unit. Good for dashboards and metrics. ms(90_000_000_000) // "1m 30s" msCompact(90_000_000_000) // "1.50m" parse(str: string): number

Parse a duration string back to nanoseconds. Returns NaN on invalid input.

Accepted units: ns, us, µs, ms, s, m, h, d
Examples: "1h 30m", "2.5s", "500ms", "1d"

Why @rabbx/ms?

Package Format 100k ops Parse 100k ops Size gzipped @rabbx/ms ~2ms ~8ms 450b ms ~60ms ~250ms 1.2kb humanize-duration ~120ms N/A 2.1kb

  1. Fast: No regex, no loops, no allocations. Just math and string concat.
  2. Small: 450 bytes gzipped. Tree-shakeable ESM.
  3. Type-safe: Full TypeScript types included.
  4. Compatible: API is a superset of the original ms package.

License

MIT

About

Ultrafast formatter and parser for time durations. 20-30x faster than the original ms package, zero dependencies, 450 bytes gzipped.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors