Observe performance entries asynchronously
Quick answer
PerformanceObserver delivers selected performance entries asynchronously to a callback. It is useful when instrumentation must react to newly reported entries instead of polling.
How it works
Check PerformanceObserver.supportedEntryTypes before requesting an optional type. Construct an observer with a callback receiving an entry list and the observer. Call observe({type:"mark",buffered:true}) to request marks including available earlier buffered entries; buffered is used with type rather than entryTypes. Use list.getEntries() in the callback. disconnect() stops delivery and discards pending records; use takeRecords before disconnect if queued entries must be collected. Callbacks should be lightweight and avoid creating an endless chain of new marks.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {let observer;try{const result=await new Promise(resolve=>{observer=new PerformanceObserver(list=>resolve(list.getEntries().map(e=>e.name).join(",")));observer.observe({type:"mark"});performance.mark("v16-observed");});console.log(result);}finally{observer?.disconnect();performance.clearMarks("v16-observed");}})()Expected console output:
v16-observedThe callback reports the newly created mark.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {performance.mark("v16-earlier",{startTime:4});let observer;try{const name=await new Promise(resolve=>{observer=new PerformanceObserver(list=>resolve(list.getEntries().find(e=>e.name==="v16-earlier")?.name));observer.observe({type:"mark",buffered:true});});console.log(name);}finally{observer?.disconnect();performance.clearMarks("v16-earlier");}})()Expected console output:
v16-earlierBuffered observation can deliver a mark created before registration.
Common mistakes
Do not assume an observer callback runs inline with mark creation or every type is supported in every browser.
Practice
Observe a uniquely named mark, record the callback contents, then disconnect and clear the mark.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Observe performance entries asynchronously — Knowledge Check
Apply the concept and reason about local example output.
Which property lists supported performance entry types?
Which option requests available prior entries for one type?
What should you do before disconnect if queued records must be retained?
What is the exact console output in Observe performance entries asynchronously example 1?
(async () => {let observer;try{const result=await new Promise(resolve=>{observer=new PerformanceObserver(list=>resolve(list.getEntries().map(e=>e.name).join(",")));observer.observe({type:"mark"});performance.mark("v16-observed");});console.log(result);}finally{observer?.disconnect();performance.clearMarks("v16-observed");}})()What is the exact console output in Observe performance entries asynchronously example 2?
(async () => {performance.mark("v16-earlier",{startTime:4});let observer;try{const name=await new Promise(resolve=>{observer=new PerformanceObserver(list=>resolve(list.getEntries().find(e=>e.name==="v16-earlier")?.name));observer.observe({type:"mark",buffered:true});});console.log(name);}finally{observer?.disconnect();performance.clearMarks("v16-earlier");}})()