I'm developing a project which makes heavy use of Javascript, and includes a lot of DOM manipulation. As the code grows, I noticed that sometimes there is a delay (about 1-2 seconds) of Javasript execution on page load under Internet Explorer (9). This delay never happened on Firefox.
For example:
// the actual code contains much more stuff
$("body").append("<p>paragraph</p>");
Although the p
was generated dynamically, it should be seen right after page loads on modern browsers. But on IE, sometimes I can clearly see the p
was being added after all static content loads, which makes the whole page jumpy.
If I restarted IE, the delay's gone. However, it would occur again on next page refresh if a lot of DOM manipulation has been done.
So I'm thinking if this was caused by memory leak
since I rarely nullify object references.
var foo = $("#foo");
foo.on("click", function() {
var bar = '<div id="bar">bar</div>';
$("body").append(bar);
// nullify bar
bar = null;
});
// nullify foo
foo = null;
Questions:
Should I nullify every object reference when they are done being used like in the example above?
If the answer to Q1 is no, when or in what condition should I nullify object references?
What else could I do to prevent memory leaks besides nullifying?
PS: I'v read some similar questions asked a few years ago, they are mostly for IE 6/7.
` and then after appending it querying it's `.position()` or `.outerHeight()`. That forces the browser to recompute the layout, and it's expensive if you do it a lot.
– Pointy Feb 23 '13 at 14:51