Briefly
Performance API is a browser API that allows you to measure the execution time of a program using various methods. It uses a very precise type of time measurement – DOM
, which operates with an accuracy of up to 5 microseconds (there are a thousand of them in one millisecond).
Example
Creating marks and measurements
Get the time elapsed since the start of navigation to the page
const t = performance.now()console.log(t)// 471359
const t = performance.now() console.log(t) // 471359
Create a named timestamp that stores the time in milliseconds since the start of navigation to the page. This is useful for measuring program performance; for example, you can calculate the difference between marks and determine the function's execution time.
performance.mark('application start')console.log(t)
performance.mark('application start') console.log(t)
Calculate the time between two marks:
const start = performance.mark('start')const finish = performance.mark('end')performance.measure('total', 'start', 'end')console.log(performance.getEntriesByName('total')[0].duration)// number of milliseconds between the 'start' and 'end' marks
const start = performance.mark('start') const finish = performance.mark('end') performance.measure('total', 'start', 'end') console.log(performance.getEntriesByName('total')[0].duration) // number of milliseconds between the 'start' and 'end' marks
Working with recorded data
Get the list of marks and measurements:
for (const entry of performance.getEntries()) { console.log(` Entry "${entry.name}", type ${entry.entryType}. Start at ${entry.startTime}ms, duration ${entry.duration}ms `)}
for (const entry of performance.getEntries()) { console.log(` Entry "${entry.name}", type ${entry.entryType}. Start at ${entry.startTime}ms, duration ${entry.duration}ms `) }
Clear the list of marks and measurements:
performance.clearMeasures()performance.clearMarks()
performance.clearMeasures() performance.clearMarks()
Or we can clear everything at once:
performance.clearResourceTimings()
performance.clearResourceTimings()
How it is written
Creating marks
A mark is the time from the beginning of the transition to the page to the creation of the mark in milliseconds. For example, from clicking on the link or after confirming the entered URL in the search bar.
When creating marks, we can pass a string as the first argument - the name of the mark. Later, we can refer to this name for searching.
const markName = 'function execution start'performance.mark(markName)const entries = performance.getEntriesByName(markName)console.log(entries)
const markName = 'function execution start' performance.mark(markName) const entries = performance.getEntriesByName(markName) console.log(entries)
The mark object contains the value mark
in the entry
field.
Creating measurements
A measurement is the time difference between two marks. A measurement takes several arguments:
- Measurement name;
- Name of the first mark - optional parameter; if not specified, the first mark will be the time from the start of navigation to the page;
- Name of the second mark - optional parameter; if not specified, the second mark will be the call to
performance
at the moment of measurement creation.. now ( )
In Firefox and some mobile browsers, calling the measure
method does not return the measurement, and you need to request it manually using get
. Check the support table.
const markOne = 'mark_1'const markTwo = 'mark_2'performance.mark(markOne)performance.mark(markTwo)performance.measure('time from navigation start to page')performance.measure('from first mark to now', markOne)performance.measure('time between two marks', markOne, markTwo)const m1 = performance.getEntriesByName('time from navigation start to page')[0]const m2 = performance.getEntriesByName('from first mark to now')[0]const m3 = performance.getEntriesByName('time between two marks')[0]console.log({ m1, m2, m3 })
const markOne = 'mark_1' const markTwo = 'mark_2' performance.mark(markOne) performance.mark(markTwo) performance.measure('time from navigation start to page') performance.measure('from first mark to now', markOne) performance.measure('time between two marks', markOne, markTwo) const m1 = performance.getEntriesByName('time from navigation start to page')[0] const m2 = performance.getEntriesByName('from first mark to now')[0] const m3 = performance.getEntriesByName('time between two marks')[0] console.log({ m1, m2, m3 })
Ways to get marks and measurements
You can get measurements and marks in three different ways:
performance
- get a list of all marks and measurements, including those recorded by the browser.. get Entries ( ) performance
- get a list of records of the specified type, e.g.,. get Entries By Type ( type ) mark
ormeasure
.performance
- get a list of records with the specified name.. get Entries By Name ( name )
More about browser-automatically recorded marks
To enhance the analysis of page performance, the browser automatically records certain marks:
navigation
– browser navigation eventsdom
,Complete load
,Event Start load
,Event End redirect
,Count dom
,Content Loaded Event Start dom
,Content Loaded Event End dom
,Interactive request
,Start response
,Start unload
,Event End unload
.Event Start resource
– contains information about resources loaded by the site. For example, you can learn about the loading of styles or execution of API requests.paint
– information about page rendering, e.g., the time of the first content render –first
,- paint first
.- contentful - paint
Any method will return an array of entries:
const mark = performance.mark('start')const measure = performance.measure('time since start', 'start')const entries = performance.getEntries()const entriesByName = performance.getEntriesByName('time since start')const onlyMarks = performance.getEntriesByType('mark')console.log(entries)console.log(entriesByName)console.log(onlyMarks)
const mark = performance.mark('start') const measure = performance.measure('time since start', 'start') const entries = performance.getEntries() const entriesByName = performance.getEntriesByName('time since start') const onlyMarks = performance.getEntriesByType('mark') console.log(entries) console.log(entriesByName) console.log(onlyMarks)
Ways to clear records
Marks and measurements with the same name do not overwrite each other. If the same name can be used in different parts of the code, for example, if names are created dynamically, it might be useful to remove previously created marks before recording new ones with the same name. You can clear recorded marks and measurements using different methods:
performance
- clear all recorded marks with the specified name. If no name is provided, all marks created by the. clear Marks ( mark _ name ) performance
method will be deleted.. mark ( ) performance
- clear all recorded measurements with the specified name. If no name is provided, all measurements created by the. clear Measures ( measure _ name ) performance
method will be deleted.. measure ( ) performance
- clear all marks related to resource loading by the browser.. clear Resource Timings ( )
const mark = performance.mark('mark')const measure = performance.measure('measurement')console.log(performance.getEntriesByName('mark').length)// 1performance.clearMarks('mark')performance.clearMeasures('measurement')console.log(performance.getEntriesByName('mark').length)// 0performance.clearResourceTimings()
const mark = performance.mark('mark') const measure = performance.measure('measurement') console.log(performance.getEntriesByName('mark').length) // 1 performance.clearMarks('mark') performance.clearMeasures('measurement') console.log(performance.getEntriesByName('mark').length) // 0 performance.clearResourceTimings()
How to understand
When you need to check the speed of code execution, conduct performance tests, or find bottlenecks — the Performance API with its convenient methods and precise measurements comes to the rescue.
The Performance API represents a registry of entries. Entries can be of different types:
mark
— named timestamp;measure
— measurement. Duration between two marks;element
— time to load elements;navigation
— for entries related to navigation on the site;resource
— time to acquire external resources (css, API requests);paint
— time of the first render (first paint), or the first render of content (first contentful paint);longtask
— time to execute a task from the LongTasks API;
The type of entry is stored in the entry
field. In manual mode, we work with marks and measurements.
In practice
Advice 1
🛠 Conveniently analyze performance using the "Performance" tab in the developer tools. Calls to performance
and performance
will be displayed in the Timings
section after profiling is recorded.

🛠 There may be a desire to write a decorator or a wrapper function for performance
and performance
and wrap the entire application in it. For example:
const withPerformanceMeasure = (markName, functionToAudit) => { performance.mark(`${markName}-before`) functionToAudit() performance.mark(`${markName}-after`) performance.measure(`${markName}-before`,`${markName}-after`)}// Script BodywithPerformanceMeasure(myApp)
const withPerformanceMeasure = (markName, functionToAudit) => { performance.mark(`${markName}-before`) functionToAudit() performance.mark(`${markName}-after`) performance.measure(`${markName}-before`,`${markName}-after`) } // Script Body withPerformanceMeasure(myApp)
This should not be done. The execution cost of the function performance
is minimal, but not zero.

Advice 2
🛠 performance
is useful for finding bottlenecks in your program. Let's consider an example where we have two functions function
and function
and we want to determine which function is slowing down our program.
When measuring, it is evident that function
is working slower, so to speed it up, it needs to be optimized.
🛠 Developer tools allow you to monitor the performance of the program in other ways as well. For example, in the Performance tab, you can record the program's operation and analyze the execution time of individual functions or rendering metrics. In the developer tools settings, you can enable FPS (frames per second) display and check the responsiveness of the interface.