Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Is it possible to combine console.timeEnd() and console.debug()?

I would like to measure time in browser javascript.

I use console.log(), console.debug() ...etc.

But console.timeEnd() behavior is similar to console.log(). It's wrote to console in case of "log" switch is on in browser's JS-Console. I need to write time to console only if debug messages has switched on in browser.

Is it possible somehow?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There's nothing built in that will do that for you, but it's very easy to implement it yourself: Maintain a Map of labels to start times, and use Date.now() or performance.now() to get the start and end times.

Very roughly:

const debugTimeMap = new Map();
const debugTime = label => {
    if (debugTimeMap.has(label)) {
        console.warn(`Timer '${label}' already exists`);
    } else {
        debugTimeMap.set(label, Date.now());
    }
};
const debugTimeEnd = label => {
    const start = debugTimeMap.get(label);
    if (start === undefined) {
        console.warn(`Invalid debugTime label "${label}"`);
    } else {
        debugTimeMap.delete(label);
        console.debug(`${label}: ${Date.now() - start} ms`);
    }
};

const debugTimeMap = new Map();
const debugTime = label => {
    if (debugTimeMap.has(label)) {
        console.warn(`Timer '${label}' already exists`);
    } else {
        debugTimeMap.set(label, Date.now());
    }
};
const debugTimeEnd = label => {
    const start = debugTimeMap.get(label);
    if (start === undefined) {
        console.warn(`Invalid debugTime label "${label}"`);
    } else {
        debugTimeMap.delete(label);
        console.debug(`${label}: ${Date.now() - start} ms`);
    }
};

debugTime("x");
setTimeout(() => {
    debugTimeEnd("x");
}, Math.floor(Math.random() * 1000));
(Look in the real console, and don't forget to turn on debug output in the console.)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!