5

I would like to run performance measurement for some critical functions on the website written on TypeScript. I'm wondering if there is any implementation of stopwatch similar to .NET System.Diagnostics.Stopwatch class in TypeScript?

Aliaksei Maniuk
  • 1,534
  • 2
  • 18
  • 30
  • Have you found something in JavaScript that works? Because you can just use that in TypeScript. Otherwise this is off-topic, since asking for recommendations is off-topic, as are requests to find libraries. – Heretic Monkey May 22 '17 at 21:44
  • There are some libraries written in JavaScript; I'm just curious if there are any in TypeScript. – Aliaksei Maniuk May 22 '17 at 21:48
  • 2
    Don't get hung up on the differences; by the time it gets to the browser, it's all JavaScript. – Heretic Monkey May 22 '17 at 21:50
  • See also https://developer.mozilla.org/en-US/docs/Web/API/Performance/now – xmojmr May 23 '17 at 06:37

4 Answers4

7

I have a simple implementation here:

/**
 * Simple timer
 */
export function timer() {
    let timeStart = new Date().getTime();
    return {
        /** <integer>s e.g 2s etc. */
        get seconds() {
            const seconds = Math.ceil((new Date().getTime() - timeStart) / 1000) + 's';
            return seconds;
        },
        /** Milliseconds e.g. 2000ms etc. */
        get ms() {
            const ms = (new Date().getTime() - timeStart) + 'ms';
            return ms;
        }
    }
}

Usage

const howLong = timer();
// do some stuff
console.log(howLong.ms);

There are lots of other implementations out there as well as the built in console.time function you can look at.

basarat
  • 261,912
  • 58
  • 460
  • 511
7

Building off of basarat's answer above, this is already built in to console.

console.time('myTask');
// code to time here
console.timeLog('myTask');
console.timeEnd('myTask');
seabass
  • 1,030
  • 10
  • 22
  • 1
    This is a great solution. In Chrome I was getting an error when console.timeLog() was called. It seems that in Chrome calling console.timeEnd() deletes the timer. Deleting the console.timeLog() fixed my issue in Chrome. --- Additional Reference: https://stackoverflow.com/questions/50524467/mytimer-dose-not-exist-in-console-timer – Brendan Sluke Apr 03 '21 at 04:45
2

This stopwatch is written in typescript and comes with type definitions. https://github.com/vcfvct/stopwatch-node

LeOn - Han Li
  • 9,388
  • 1
  • 65
  • 59
0

Since the OP asked specifically for something similar to .NET's Stopwatch, the @tsdotnet/stopwatch package might be a good option.

First, install package in your npm project: npm i @tsdotnet/stopwatch

Code example:

import Stopwatch from "@tsdotnet/stopwatch"

(async () => {
const stopwatch = Stopwatch.startNew();

    // Simulates a delay of 1000 milliseconds.
    await new Promise(r => setTimeout(r, 1000));

    console.log(stopwatch.elapsedMilliseconds);
})();

Please, notice that your tsconfig.json needs to be targeting at least es2015 due to the use of promises in this example. This is not a requirement because of @tsdotnet/stopwatch.

Christiano Kiss
  • 399
  • 4
  • 9