安装
npm install precise
yarn add precise
pnpm add precise
bun add precise
README

precise

Nanosecond-precision timer for Node.js using process.hrtime.bigint().

Why precise over performance.now()?

While performance.now() provides millisecond precision with sub-millisecond fractional parts, precise delivers true nanosecond accuracy using Node.js's process.hrtime.bigint() API:

Method Precision Resolution
performance.now() Milliseconds ~0.1-1ms
precise Nanoseconds ~1ns

For performance-critical applications measuring code execution, benchmarking, or profiling, the 6 decimal places of additional precision can reveal timing differences that performance.now() simply cannot detect.

Using the factory

import {precise} from "precise";
const timer = precise().start();
setTimeout(() => console.log(timer.stop().diff()), 1000);

Using the Class

import {Precise} from "precise";
const timer = new Precise().start();
setTimeout(() => console.log(timer.stop().diff()), 1000);
import {Precise} from "precise";
class MyTimer extends Precise {}

Testing

Precise has 100% code coverage with its tests.

ℹ start of coverage report
ℹ ----------------------------------------------------------
ℹ file      | line % | branch % | funcs % | uncovered lines
ℹ ----------------------------------------------------------
ℹ ----------------------------------------------------------
ℹ all files | 100.00 |   100.00 |  100.00 | 
ℹ ----------------------------------------------------------
ℹ end of coverage report

API

diff(ms = false)

Returns the time delta between start() & stop(); setting optional parameter to true will return the delta as milliseconds

elapsed(ms = false)

Returns the elapsed time since start() without stopping the timer

format(ms = false, delta)

Returns a human-readable string of the elapsed time. Optional delta parameter allows formatting a custom nanosecond value.

reset()

Reset a timer

start()

Starts a timer

stop()

Stops a timer

License

Copyright (c) 2026 Jason Mulligan Licensed under the BSD-3 license

版本列表
5.0.1 2026-03-29
5.0.0 2026-03-28
4.0.3 2023-10-10
4.0.2 2023-10-06
4.0.1 2023-10-05
4.0.0 2023-10-04
3.0.0 2023-10-04
2.0.1 2023-01-03
2.0.0 2022-09-05
1.1.0 2015-09-03
1.0.1 2014-04-25
1.0.0 2014-04-25