Skip to content

Latest commit

 

History

History
executable file
·
53 lines (40 loc) · 1019 Bytes

javascriptDebugging.md

File metadata and controls

executable file
·
53 lines (40 loc) · 1019 Bytes

Log

// Write to console
console.log();

// Show DOM element
console.dir();

// Display a table. Takes an array of objects.
console.table(array);

Execution

// Display the call stack of a function
console.trace();

// Track execution time
console.time("point"); // undefined
console.timeEnd("point"); // point: 1337.42 ms

// Count the number of executions
console.count("foo"); // foo: 1
console.count("foo"); // foo: 2
console.countReset("foo"); // undefined
console.count("foo"); // foo: 1

Memory

// Heap size
console.memory;

// Show memory usage
setInterval(() => {
    const used = process.memoryUsage().heapUsed / 1024 / 1024;
    console.log(`Script uses ${Math.round(used * 100) / 100} MB`);
}, 1000);

Test

// Avoid if-else statements
function greaterThan(a, b) {
    console.assert(a > b, { message: "a is not greater than b", a: a, b: b });
}

greaterThan(2, 1); // a is not greater than b, a: 2, b: 1