How to use Performance API In Javascript

The Performance API gives an accurate way to measure time. Its now() method tells the current time very precisely, useful for checking performance or comparing speeds.

Example: To demonstrate how to use JavaScript’s Performance API to obtain the current time in milliseconds.

HTML
<!DOCTYPE html>
<html>
<head>
<title>Performance milliseconds example </title>
</head>
<body>

<script>
  const milliseconds = performance.now();
  document.write("Milliseconds: " + milliseconds);
</script>
  
</body>
</html>

Output:

performance API to obtain current time in milliseconds

How to Get Milliseconds in JavaScript?

JavaScript provides various methods to precisely measure time down to the millisecond, including Date.now(), performance.now(), and the Date object’s getTime() method. These tools enable developers to effectively manage time in their programming tasks.

These are the following methods to get milliseconds in JavaScript:

Table of Content

  • Date Object
  • Using Performance API
  • Using `Date.now ( )`

Similar Reads

Date Object

In JavaScript, you can easily get the milliseconds by creating a new Date object and using its getMilliseconds() method, which generates the milliseconds part of the current time. This straightforward approach makes accessing time information for developers easier....

Using Performance API

The Performance API gives an accurate way to measure time. Its now() method tells the current time very precisely, useful for checking performance or comparing speeds....

Using`Date.now ( )`

You can quickly get the current time in milliseconds using the Date class’s now() method. It’s a direct way to get accurate time information whenever you require it....

Contact Us