console.time And console.count Structure Debug Output

console.time/timeEnd bracket a span under one label, timeLog prints mid-span checkpoints, and count tracks how often a label fired with deterministic output.

JavascriptCli Tooling

Code

javascriptsnippet
17 lines
// console.time/timeEnd bracket one labeled span; timeLog logs mid-span checkpoints
console.time("spin");
let total = 0;
for (let i = 0; i < 200_000; i++) {
  total += i;
}
console.timeLog("spin"); // 中途打点,计时继续
console.timeEnd("spin"); // 停表并输出
console.log("total:", total);

// console.count 数同名标签的调用次数:确定性输出
console.count("hits");
console.count("hits");
console.count("miss");
console.count("hits");
console.countReset("hits");
console.count("hits");

Output

Captured from a sandboxed run of this snippet in the build pipeline.

spin: 2.345ms
spin: 6.448ms
total: 19999900000
hits: 1
hits: 2
miss: 1
hits: 3
hits: 1

How the snippet works

for (let i = 0; i < 200_000; i++) is the loop that carries the per-item work.

Top-level bindings total hold the demo state — nothing sits inside a function scope.

Applying it

Output lines are prefixed (spin, total:, hits), making each step’s effect easy to spot.

Statements that matter

console.timeLog("spin"); // 中途打点,计时继续
console.timeEnd("spin"); // 停表并输出
console.log("total:", total);
console.countReset("hits");
console.count("hits");

Interactive Tools for This Topic

FAQ

Any setup needed beyond the language runtime?
None — the language runtime is the only requirement. Its state lives in module-level bindings: `total`.