npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

itty-time

v1.0.6

Published

Ultra-small (~390 bytes) library for TTL date math and converting ms durations to and from strings.

Downloads

1,384

Readme

Version Bundle Size Coverage Status Issues Discord

v1 Documentation  |   Discord


Ultra-small (~390 bytes) library for TTL date math and converting ms durations to and from strings.

Features

  • Tiny. The entire library is ~390 bytes, and fully tree-shakeable.
  • Convert string durations to ms/seconds.
  • Convert ms to human-readable string durations.
  • Add durations to dates.

Comparison to other top-rated libraries

| library | string to ms | ms to string | date math | size1 | --- | :-: | :-: | :-: | :-: | | itty-time | ✅ | ✅ | ✅ | 391b | | @lukeed/ms | ✅ | ✅ | ❌ | 435b | | ms | ✅ | ❌ | ❌ | 938b | | pretty-ms | ❌ | ✅ | ❌ | 1.04kB | | humanize-duration | ❌ | ✅ | ❌ | 6.74kB |

1: minified and gzipped  

Performance

The only function most folks care about in terms of raw performance is string to ms conversion. In this, itty stacks up pretty well, being significantly faster than ms, but falling to the insanely-optimized @lukeed/ms.

image

Moral of the story, probably don't use ms.

Use Luke's if you want the absolute fastest parsing, or itty if you want some of the other functions as well. If you're byte-counting, itty wins again, but if you're byte-counting that hard, you're probably better off with raw ms math if you can stomach it.


seconds/ms

TTL math is a maintenance nightmare. It's a pain to write, a pain to read, and when you update the math later, you'll probably forget to update the comment, causing all sorts of mayhem.

const TTL = 2 * 7 * 24 * 60 * 60 * 1000 // 2 weeks, right?

Here's a better way.

import { ms, seconds } from 'itty-time'

// to seconds
seconds('2 weeks') // 1209600

// to milliseconds
ms('2 weeks') // 1209600000

duration

Of course, we sometimes need to go the other direction. Want to tell a user how long ago something happened? How much time they have left?

You could build it yourself, or import the fantastic humanize-duration library that inspired this, but at 6.3kB1, it's over 20x the size of this 280 byte function.

1: of course humanize-duration can also do much, much more.

import { duration } from 'itty-time'

duration(3750000)
// "1 hour, 2 minutes, 30 seconds"

// limit number of segments returned
duration(3750000, { parts: 2 })
// "1 hour, 2 minutes"

// change the delimiter
duration(3750000, { join: ' --> ' })
// "1 hour --> 2 minutes --> 30 seconds"

// or get the raw components
duration(3750000, { join: false })
/*
  [
    ['hour', 1],
    ['minutes', 2],
    ['seconds', 30]
  ]
/*

datePlus

Sometimes you need a TTL for some point in the future, but sometimes you need the actual date. You could convert it all yourself... or use this.

import { datePlus } from 'itty-time'

// from right now
datePlus('2 months')

// or from a different date
datePlus('2 months', datePlus('1 week'))