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.
npm i @rabbx/msimport 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') // -90000000ms(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- microsecondsms- millisecondss- secondsm- minutesh- hoursd- 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"
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
- Fast: No regex, no loops, no allocations. Just math and string concat.
- Small: 450 bytes gzipped. Tree-shakeable ESM.
- Type-safe: Full TypeScript types included.
- Compatible: API is a superset of the original
mspackage.
License
MIT