Master JavaScript Debugging: Console Tricks to Trace, Table, and Profile Code
This guide explores essential JavaScript console methods—trace, table, time, and profile—to help developers debug call stacks, visualize objects, measure execution time, and analyze CPU usage, complete with code examples and visual illustrations.
Trace Stack
Use console.trace() to track the call stack and see exactly where a function was invoked.
function func_A() {
func_B(1, 2);
}
function func_B(arg1, arg2) {
console.trace();
}
func_A();Display Object Information as Table
Printing objects with console.log() can be hard to read; console.table() formats them as a readable table.
var books = [
{ title: "Java", author: "Abc" },
{ title: "C", author: "Obj" },
{ title: "C++", author: "Cof" }
];
console.log(books);Result shown in the image below.
Using console.table() improves readability.
console.table(books);Measure Execution Time
Use console.time() and console.timeEnd() to measure how long a block of code runs.
console.time('timer');
for (var i = 0; i < 100000; i++) {
// ...
}
console.timeEnd('timer');Profile CPU Consumption
console.profile()and console.profileEnd() let you see CPU usage and identify time‑consuming code.
function func_A(num){
for(var i=0;i<num;i++){
console.log('A');
}
}
function func_B(x,y){
for(var i=0;i<x;i++){
for(var j=0;j<y;j++){
console.log('B');
}
}
}
function test(){
func_A(100);
func_B(3,10);
}
console.profile('test');
test();
console.profileEnd();The profiling result shows each function’s self time (time spent in the function itself) and total time (including time spent in called functions).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
