brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: queryObjects() in the DevTools console finds every instance of a class

Devtools
~/tips/tip-devtools-queryobjects-instances

queryObjects(Promise)

Count the live objects of one type while hunting a memory leak.

Sometimes the question is simply how many of these objects exist right now. If a modal gets created every time you open it and never goes away, I want to see that pile with my own eyes.

In the Chrome DevTools console, pass a constructor or class:

queryObjects(Promise)
queryObjects(HTMLElement)

The console prints an array with the objects made by that constructor. Expand it to look at their properties. For your own code it works the same way:

class Modal {}
const a = new Modal(), b = new Modal();
queryObjects(Modal)

A leak check goes like this: run it, open and close the dialog ten times, run it again. If the count climbs by ten and stays there, something is still holding on to those objects. A heap snapshot in the Memory panel can then show you what.

This is a console helper, so calling it from your page’s own scripts throws an error. It also only looks in the execution context picked in the console, which means objects inside an iframe need that frame selected in the dropdown first. And do not read too much into one run: an object that is still referenced for a good reason shows up too.

Further reading: Chrome DevTools: Console utilities API

More Devtools tips Clear site data for one site from Chrome DevTools user.id === 42 $x(path [, startNode]) @media (prefers-color-scheme: dark) { inspect(document.querySelector('footer')) All Devtools tips (22) All topics

Link