jackmoore / autosize

Autosize is a small, stand-alone script to automatically adjust textarea height to fit text.
http://www.jacklmoore.com/autosize/
MIT License
5.1k stars 702 forks source link

Allow detached autosize DOM elements to be garbage collected #410

Open blutorange opened 2 months ago

blutorange commented 2 months ago

The proper way is to use autosize.destroy when an autosize instance is no longer needed.

However, sometimes it may be hard to explicitly destroy existing widget instances. See for this issue from another widget where the DOM element or DOM container element is simply removed. For example, try creating a simple demo page with a div container and some textareas with autosize, then remove the div container from the DOM. The assignedElements still contains all entries, and the memory tab of the dev tools shows many detached DOM elements that weren't garbage collected.

This happens because of 2 reasons: (a) The assignedElements map keeps a hard reference to all DOM textareas and the autosize instances; (b) each instances adds a global resize event listener to the window, and the event listener captures the textarea DOM element with a closure.

There is a simple change that solves these issues:

lofcz commented 2 months ago

thanks for working on this, pulled locally and tested - works fine. Hope this gets merged.