Time Module

Monotonic timing helpers for JavaScript-side performance measurements

Sections

Terminology & Glossary
πŸ“– Documentation
Navigation
31 sectionsv0.1
πŸ“„ Time Module β€” glaze help time-module
time-module

Time Module

Monotonic timing helpers for JavaScript-side performance measurements

Topictimemodulesgojajavascriptgoja-repl

The time module provides lightweight monotonic clock helpers. It is not a wall-clock date library; it is designed for benchmarking and measuring elapsed intervals inside a Goja runtime.

The counter starts when the module is first loaded into a runtime, so now() values are relative to that point.

JavaScript usage

const time = require("time");

const start = time.now();
for (let i = 0; i < 100000; i++) {
  Math.sqrt(i);
}
const elapsed = time.since(start);
console.log(`took ${elapsed} ms`);

Module API

now()

Returns the number of milliseconds elapsed since the module was initialized in the current runtime. The value is a monotonic float and never decreases.

since(startMs)

Given a previous value returned by now(), returns the delta in milliseconds. Equivalent to now() - startMs but expressed as a helper.

Design notes

time uses Go's time.Since over a baseline recorded at module load time. This avoids the overhead and non-monotonic behavior of wall-clock date APIs. If you need calendar dates, format strings, or time zones, use JavaScript's built-in Date object instead.

Troubleshooting

ProblemCauseSolution
Negative since() resultThe startMs argument came from a different runtime instanceCapture now() inside the same runtime before measuring
Very small now() valuesThe module was loaded seconds agoThis is expected; values are relative to module initialization