Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Last active January 20, 2019 06:38
Show Gist options
  • Select an option

  • Save samarpanda/d8feb418650e338615ec2eee5bfe765e to your computer and use it in GitHub Desktop.

Select an option

Save samarpanda/d8feb418650e338615ec2eee5bfe765e to your computer and use it in GitHub Desktop.

Browser LongTask API

https://www.w3.org/TR/longtasks/

const observer = new PerformanceObserver((list) => {
  for(const entry of list.getEntries()){
    console.log(`Long Task - time: ${entry.startTime + entry.duration}, attribution: ${JSON.stringify(entry.attribution)}`)
    //sendDataToAnalytics('Long Task', {time: entry.startTime + entry.duration, attribution: JSON.stringify(entry.attribution)});
  }
})

observer.observe({entryTypes: ['longtask']})

Old way

This was depricated as we need to ensure we are not detoriating performance while monitoring for performance bottlenecks.

(function detectLongFrame(){
	var lastFrameTime = Date.now()
	requestAnimationFrame(function(){
		var currentFrameTime = Date.now()

		if(currentFrameTime - lastFrameTime > 50){
			//Report long frame here ...
		}

		detectLongFrame(currentFrameTime)
	})
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment