Introducing Niobe

A simple way to manage time-based intervals in your applications.

  • Published:
  • Tags: Software engineering
  • Reading time: 2 minutes

The problem

How often do you write code like this?

setTimeout(() => /* Do something */, 1_000 * 60 * 2.5);

// or

setTimeout(() => /* Do something */, 150_000);

Both of these do the same thing, they execute the code inside the callback function after the specified time has elapsed, but how long is it?

Unless you are blessed with the ability to do mental mathematics (quickly), or have memorised common sums, you probably have to stop and think what 1_000 * 60 * 2.5 or 150_000 is. For the curious, it is 2 minutes and 30 seconds expressed in milliseconds.

You might be thinking “is this a big deal?“. The answer is no, not really. After all, it doesn’t take long to copy/paste it into a calculator, console or nudge the AI tool of your choice to figure it out.

Often in large codebases you’ll start to abstract these numbers into constants, like const TWO_AND_HALF_MINUTES = 1_000 * 60 * 2.5; and encourage the team to use them instead. Not only does this make the code more readable, you’ll also likely reduce the chance of errors and maybe some optimisations in your build through reuse.

import { TWO_AND_HALF_MINUTES } from './time.js';

setTimeout(() => /* Do something */, TWO_AND_HALF_MINUTES);

Straight away this is much better at the point of consumption, but what happens when you need to increase the interval to 3 minutes? Given this is a shared constant, you’ll likely have to introduce a new constant, or maybe you ignore all the rules and wedge and increase on the existing one.

import { TWO_AND_HALF_MINUTES } from './time.js';

setTimeout(() => /* Do something */, TWO_AND_HALF_MINUTES + 1_000 * 30);

Eeek! This is now a mess. We’re back to square one.

The solution

Write code that a future-you will thank, not curse, you for.

One key principle I’ve learnt and tried to employ is that, to maintain code, it must be maintainable - Write code that a future-you will thank, not curse, you for.

I had some rare free time over a weekend so I decided to try and see if I could come up with something better. I wanted to create a simple way to manage time-based intervals in my applications, so I created Niobe .

Niobe provides a simple, unified, human readable way to provide durations. The same duration can be expressed as:

import { minutes, seconds } from 'niobe';

setTimeout(() => /* Do something */}, minutes(2) + seconds(30));

There are no project-specific constants to import, no need to remember magic numbers, just clear, expressive code that conveys intent at a glance.

Equally, a constant can still be created, but extending it is instantly more readable:

import { minutes, seconds } from 'niobe';

const TWO_AND_HALF_MINUTES = minutes(2) + seconds(30);

setTimeout(() => /* Do something */}, TWO_AND_HALF_MINUTES + seconds(30));

Since starting Niobe, the API has grown and now supports further utilities to parse durations, split them into their components and convert between different time units. There is even a range of common constants .

Please take a look at the README and please consider contributing and giving it a star if you find it useful!