Memory Leak on element.onclick

30 views
Skip to first unread message

Mohit Kanika

unread,
May 1, 2020, 11:41:38 PM5/1/20
to Zakas Books
Hi Nicholas,

From your book "PROFESSIONAL JAVASCRIPT FOR WEB DEVELOPERS third edition", on page 227(Memory Leaks). You have provided below pattern of code to avoid memory leaks. 
Could you please confirm that below pattern is required in Chrome or Firefox where DOM is implemented as JS object and Mark & sweep is used as GC? i.e. do I need to always set element to null after I assign event handlers on an element?

    function assignHandler() {
        var element = document.getElementById("someElement");
        var id = element.id;
        element.onclick = function () {
            alert(id);
        };
        element = null;
    }


Nicholas Zakas

unread,
May 5, 2020, 11:00:10 AM5/5/20
to zakas...@googlegroups.com
I think so but I’m not 100% sure. Browsers now have devtools that allow you to inspect memory, so I’d suggest using that to verify (these didn’t exist when the book was written).

--
You received this message because you are subscribed to the Google Groups "Zakas Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zakasbooks+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/zakasbooks/8c0ec4ae-bf69-43d4-b8f3-5f46850fca48%40googlegroups.com.

Mohit Kanika

unread,
May 6, 2020, 9:20:53 PM5/6/20
to Zakas Books
Thank you Nicholas, will look into devtool option.


On Tuesday, May 5, 2020 at 11:00:10 AM UTC-4, Nicholas C. Zakas wrote:
I think so but I’m not 100% sure. Browsers now have devtools that allow you to inspect memory, so I’d suggest using that to verify (these didn’t exist when the book was written).
On Fri, May 1, 2020 at 8:41 PM Mohit Kanika <kanik...@gmail.com> wrote:
Hi Nicholas,

From your book "PROFESSIONAL JAVASCRIPT FOR WEB DEVELOPERS third edition", on page 227(Memory Leaks). You have provided below pattern of code to avoid memory leaks. 
Could you please confirm that below pattern is required in Chrome or Firefox where DOM is implemented as JS object and Mark & sweep is used as GC? i.e. do I need to always set element to null after I assign event handlers on an element?

    function assignHandler() {
        var element = document.getElementById("someElement");
        var id = element.id;
        element.onclick = function () {
            alert(id);
        };
        element = null;
    }


--
You received this message because you are subscribed to the Google Groups "Zakas Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zakas...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages