Measuring time - console.time
suggest changeconsole.time() can be used to measure how long a task in your code takes to run.
console.time(label) starts a timer. console.timeLog(label) logs the elapsed milliseconds and leaves the timer running. console.timeEnd(label) logs the elapsed time and removes the timer. A second timeEnd for that label does not print another measurement. In Node it warns No such label. A missing label uses "default". The millisecond counts below are one run, not a fixed result. alert is browser-only.
Example 1:
console.time('response in');
alert('Click to continue');
console.timeLog('response in');
alert('One more time');
console.timeEnd('response in');
will output:
response in: 774.967ms
response in: 1402.199ms
Example 2:
const elms = document.getElementsByTagName('*'); //select all elements on the page
console.time('Loop time');
for (let i = 0; i < 5000; i++) {
for (let j = 0, length = elms.length; j < length; j++) {
// nothing to do ...
}
}
console.timeEnd('Loop time');
will output:
Loop time: 40.716ms
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents