Microsecond Time Utility
The `microseconds` package provides a utility to generate and parse timestamps with microsecond precision. It currently stands at version 0.2.0, with its last update occurring approximately six years ago (December 2019). This indicates that the package is no longer actively maintained. It intelligently adapts to the execution environment, utilizing `process.hrtime` for high-resolution timing in Node.js and `performance.now()` in browsers. If neither is available, it falls back to `Date.now() * 1000` for a lower-resolution microsecond timestamp. Key functionalities include `μs.now()` for current microsecond timestamps, `μs.parse()` to convert a microsecond value into a structured object or human-readable string, and `μs.since()` to calculate elapsed time. Due to its abandoned status, developers should consider more modern and actively maintained alternatives for critical applications requiring robust time precision.
Common errors
-
TypeError: microseconds is not a function
cause Attempting to `import μs from 'microseconds'` in an ESM module, or incorrectly destructuring a CommonJS module that exports a single function/object.fixUse the correct CommonJS `require` syntax: `const μs = require('microseconds')`. -
SyntaxError: Cannot use import statement outside a module
cause Trying to use `import μs from 'microseconds'` in a Node.js script that is interpreted as CommonJS (e.g., a `.js` file without `"type": "module"` in `package.json`).fixEither convert your project to an ES module (by adding `"type": "module"` to `package.json` and potentially refactoring other `require`s) or revert to the CommonJS `require()` statement for this package: `const μs = require('microseconds')`.
Warnings
- breaking This package is no longer maintained. It was last published over six years ago, meaning it does not receive updates for new Node.js versions, browser API changes, or security fixes. Users should be aware of potential compatibility issues or unaddressed vulnerabilities.
- gotcha The package is CommonJS-only. Attempting to use ESM `import` syntax (`import μs from 'microseconds'`) directly in a pure ES module environment will result in errors.
- gotcha Precision and resolution can vary across environments. While it attempts to use high-resolution timers (`hrtime`, `performance.now`), it falls back to `Date.now() * 1000` if those are unavailable. This fallback significantly reduces actual microsecond precision.
Install
-
npm install microseconds -
yarn add microseconds -
pnpm add microseconds
Imports
- μs
import μs from 'microseconds'
const μs = require('microseconds') - μs.now
const now = μs.now()
- μs.parse
const parsed = μs.parse(timestamp)
Quickstart
const μs = require('microseconds');
// Get current timestamp in microseconds
const now = μs.now();
console.log(`Current microseconds: ${now}`);
// Example output: Current microseconds: 1713540843123456
// Simulate some work
for (let i = 0; i < 10000; i++) {
Math.sin(i);
}
// Get time elapsed since 'now'
const elapsed = μs.since(now);
console.log(`Time elapsed (microseconds): ${elapsed}`);
// Example output: Time elapsed (microseconds): 12345
// Parse a microsecond value into an object
const parsed = μs.parse(123456789123456);
console.log('Parsed object:', parsed);
// Example output: Parsed object: { microseconds: 456, milliseconds: 123, seconds: 89, minutes: 23, hours: 14, days: 1428 }
// Convert parsed object to a human-readable string
console.log('Parsed string:', parsed.toString());
// Example output: Parsed string: 1428 days 14 hours 23 minutes 89 seconds 123 milliseconds 456 microseconds
// Parse a smaller value
console.log(μs.parse(5000).toString()); // 5 milliseconds
console.log(μs.parse(1).toString()); // 1 microsecond