Given a loader:
function loader(src, callback, fail) {
let s = document.head.appendChild(document.createElement('script'));
s.type = "text/javascript";
s.src = src;
s.onload = function() {
callback()
s.onload = null; //useful?
s = null; //or maybe this?
}
s.onerror = fail
}
Does having a line s.onload = null
benefit from GC free some memory?
Yes, it does benefit the GC to set the properties to null
. Doing so removes the references from the element (that is contained in the DOM) to the handler function, and given that it probably is the only reference to the function it makes the function eligible for collection. However, unless the function is a closure over variables retaining a large block of memory, this is very unlikely to have a lot of impact.
You might also want to remove the s
element from the DOM in the callback, making it garbage-collectable as well.