src/utils.js
function time (name) {
  const t = timers[name];
  if (t) {
    let elapsed = Date.now() - t;
    if (elapsed < 1000) return `${elapsed}ms`;
    if (elapsed < 60 * 1000) return `${elapsed / 1000}s`;

    elapsed /= 1000;
    const h = Math.floor(elapsed / 3600);
    const m = Math.floor((elapsed % 3600) / 60);
    const s = Math.floor((elapsed % 3600) % 60);

    delete timers[name];

    return `${h}h ${m}m ${s}s`;
  } else {
    timers[name] = Date.now();
  }
}
function time (name) {
  const t = timers[name];
  if (t) {
    let elapsed = Date.now() - t;
    if (elapsed < 1000) return `${elapsed}ms`;
    if (elapsed < 60 * 1000) return `${elapsed / 1000}s`;

    elapsed /= 1000;
    const h = Math.floor(elapsed / 3600);
    const m = Math.floor((elapsed % 3600) / 60);
    const s = Math.floor((elapsed % 3600) % 60);

    delete timers[name];

    return `${h}h ${m}m ${s}s`;
  } else {
    timers[name] = Date.now();
  }
}
function time ( name ) { const t = timers [ name ] ; if ( t ) { let elapsed = Date . now ( ) - t ; if ( elapsed < 1000 ) return ` ${ elapsed } ` ; if ( elapsed < 60 * 1000 ) return ` ${ elapsed / 1000 } ` ; elapsed /= 1000 ; const h = Math . floor ( elapsed / 3600 ) ; const m = Math . floor ( ( elapsed % 3600 ) / 60 ) ; const s = Math . floor ( ( elapsed % 3600 ) % 60 ) ; delete timers [ name ] ; return ` ${ h } ${ m } ${ s } ` ; } else { timers [ name ] = Date . now ( ) ; } }
Like console.time but better. Instead of printing the value returns it.
Displays 1h 10m 10s notation for times above 60 seconds.
Uses a global timers variable to keep track of timers.
On the first call sets the time, on the second returns the value

@param {string} name Timer name
Like console . time but better . Instead of printing the value returns it . Displays 1h 10m 10s notation for times above 60 seconds . Uses a global timers variable to keep track of timers . On the first call sets the time on the second returns the value