Console Count Group And Time Diagnostics

Tally labeled calls with console.count, nest logs with group and groupEnd, measure spans with time and timeEnd, and assert only prints on failure.

JavascriptCli Tooling

Code

javascriptsnippet
26 lines
// console.count:同一标签自动累加,不同标签各计各的
console.count('request');
console.count('request');
console.count('request:parse');
console.count('request');

// console.group:折叠输出层级,groupEnd 退出
console.group('import pipeline');
console.log('read 3 files');
console.group('transform');
console.log('strip comments');
console.log('rewrite imports');
console.groupEnd();
console.log('write bundle');
console.groupEnd();

// console.time/timeEnd:测量同标签耗时,输出 "scan: <ms>ms"
console.time('scan');
let checksum = 0;
for (let i = 0; i < 100_000; i++) checksum = (checksum + i) % 97;
console.timeEnd('scan');

// console.debug 走 verbose 级别,避免淹没默认输出
console.debug('checksum:', checksum);
// console.assert 只在条件为假时打印错误,为真时静默
console.assert(checksum !== 0, 'checksum unexpectedly zero');

Output

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

request: 1
request: 2
request:parse: 1
request: 3
import pipeline
  read 3 files
  transform
    strip comments
    rewrite imports
  write bundle
scan: 1.129ms
checksum: 28

Counting by label

console.count maintains one counter per label: request is counted three times, request:parse once — the labels tally independently and the output shows the running numbers.

Nesting with group

console.group(‘import pipeline’) opens a collapsible section, an inner group(‘transform’) nests two log lines inside it, and the matching groupEnd calls close both. The hierarchy is purely presentational but makes multi-stage logs readable.

Timing and level control

console.time(‘scan’) starts a timer that console.timeEnd(‘scan’) closes with the elapsed milliseconds. console.debug goes to the verbose level so checksum noise stays out of default output, and console.assert only prints when its condition is false — a zero checksum would print, the actual nonzero one stays silent.

Interactive Tools for This Topic

FAQ

Does console.assert print when the condition is true?
No — it only prints an error when the condition is false; a true condition stays silent, which the demo relies on for its nonzero checksum.
What is the difference between console.debug and console.log?
debug routes to the verbose level of most consoles, so its output is hidden unless verbose messages are enabled.