skip to Main Content

Javascript – JS GC in closure

hi i have a query and was unable to get exact answer. I have a below closure function function closure() {let innerVal; return function() {innerVal = innerVal+10}} let inst = closure(); inst(); console.log('end'); will innerVal be cleared during GC after…

VIEW QUESTION

Javascript – Would event listeners prevent garbage collecting objects referenced in outer function scopes?

Let's assume I have the following code: (function () { const largeObject = provideSomeLargeObject(); const largeStaticListOfElements = document.querySelectorAll('span'); const someElementThatWillBeRemoved = document.getElementById('i-will-be-removed'); const elms = document.getElementsByClassName('click-me'); for (let i = 0; i < elms.length; ++i) { const anotherLargeObject = provideAnotherLargeObject();…

VIEW QUESTION

Html – Why isn't my custom element being garbage-collected?

Consider a webpage that contains a button which triggers the opening of a modal: <dialog></dialog> <button>Open modal</button> const trigger = document.querySelector(`button`); const wrapper = document.querySelector(`dialog`); trigger.addEventListener(`click`, () => { wrapper.replaceChildren(new Modal()); wrapper.showModal(); }); The modal is a custom element: class…

VIEW QUESTION

Javascript – How to use FinalizationRegistry to clear interval inside a class constructor when object is destroyed?

I have this code created with help from ChatGPT: class Thing { #intervalId; #finalizationRegistry; constructor() { console.log('constructor()'); let count = 0; this.#intervalId = setInterval(() => { console.log(`Test ${count++}`); }, 1000); this.#finalizationRegistry = new FinalizationRegistry(() => { this.#destructor(); }); this.#finalizationRegistry.register(this); }…

VIEW QUESTION
Back To Top
Search