2

I am coding a Chrome extension. I have a $.post() command that runs on a timer in the background (setInterval). The callback invokes a parse function:

function parseData(new_data) {
  new_data = $.parseJSON( new_data );
  for(var x=0; x<new_data.length; x++) {
    var obj = new CustomObj( new_data[x] );
    // I commented out code here in order to help isolate the problem.
  }
}

CustomObj is prototyped in a typical JS manner...

function CustomObj(data){
   this.data = data;
}
CustomObj.prototype.getName = function() {
  return this.data.name;
}
// Of course, there are a few more methods here...

The problem: The extension will cause major lag. If I set it to do the $.post() every 10s (simply to speed up the appearance of the problem), within 5 minutes refreshing any tab in Chrome will show "Waiting on [My Extension]" for about 30s. Eventually the browser will more or less lock up.

What I've discovered: if I comment out the innards of the for() loop, everything is just peachy. No lag ever. If I simply put the above line in the for(); loop (creating the CustomObj), the problem returns.

It seems like a garbage collection problem, as far as I can tell. I've tried implicitly defining the obj variable as well as explicitly deleting it (though Deleting Objects in JavaScript makes me believe that delete is insufficient ). Nothing seems to work.

Thanks.

Community
  • 1
  • 1
Zane Claes
  • 14,732
  • 15
  • 74
  • 131
  • I don't know, maybe the reuse of new_data is causing problems? Or is it that you're looping through a JSON object? Chrome's heap profiler (https://developers.google.com/chrome-developer-tools/docs/heap-profiling) would probably help. – gengkev Apr 21 '12 at 01:26
  • (Wait, aren't you that person who wrote Start Google Plus?...nevermind) – gengkev Apr 21 '12 at 01:28

0 Answers0