I'm writing a simple chrome extension and I've encountered very weird issue. I have a code that I inject like this
element.src = chrome.runtime.getURL('inject.js');
document.head.appendChild(element);
Injects.js itself:
(function() {
//my code here
}());
The code itself is nothing special, I use code similar to monkeyPatch to patch some original page functions and receive event data from them and my code on its own only has one anonymous function setTimeout{}
, the rest is just data/DOM manipulation.
Now, the weird thing is if I change the function I wrap my code into to IIFE example:
(function () {
//my code
})();
I start to get insane memoryleak, like, few Gb in 5-7 minutes.
I'm not very verse in JS and its ecosystem - what could cause that? I'd like to understand it and avoid similar issues it in future. Let me know if more context needed.
EDIT:
1. someone linked:
Location of parenthesis for auto-executing anonymous JavaScript functions?
Interesting how it mentions: Does the former take up memory by leaving around a global, anonymous function?
2. also this is interesting: https://stackoverflow.com/a/3783287/8313379
None of these answer my question which is valid - 2Gb leaked memory in 10 minutes still here.