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.
Code
// 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: 1How 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
Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.
FAQ
- Any setup needed beyond the language runtime?
- None — the language runtime is the only requirement. Its state lives in module-level bindings: `total`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.