A quick and easy way to measure the performance of your method in JavaScript and TypeScript.

Image for post

Photo by  Dawid Małecki on  Unsplash

Performance Measurement in JavaScript

Measuring the performance of a certain method in JavaScript or any other language is actually pretty simple and generally can be split up into 4 parts.

  • Get the current time = Start time
  • Run the method code
  • Get the current time = End time
  • Subtract the end time from the start time = Run duration of your method

So what you would do in your JavaScript is shown in the next snippet:

const myMethod = () => {
    const startTimeInMs = new Date().getTime();

    // Do some heavy computation
    // ..........
    // ////////////////////////////////////////

    const endTimeInMs = new Date().getTime();
    const durationInMs = endTimeInMs - startTimeInMs;

    console.log(`myMethod took ${durationInMs} ms`);

#programming #typescript #javascript

An Easy Way To Measure Performance In JavaScript/TypeScript
1.60 GEEK