Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere is memory leak in JavaScript / jquery?
    text
    copied!<p>I have two singletons:</p> <p>Search - performs the search functionality</p> <p>Topic - presentation of topics (search results)</p> <pre><code>var Search = new function () { this.GetTopics = function () { var query = $("#globalSearch").val(); $.ajax({ url: 'Search/GetTopics', dataType: 'json', data: { query: query }, success: function (result) { var links = $("&lt;ul /&gt;") .click(function (event) { Search.OpenTopicContent(event); }); $.each(result.Topics, function (key, value) { links.append( Topic.FormatTopic(value.Name, value.Id, value.Rank) ); }); $("#searchResult").empty(); $("#searchResult").html(links); } } }(); </code></pre> <p>This is Topic singleton:</p> <pre><code>var Topic = new function () { this.FormatTopic = function (name, id, rank) { var li = $("&lt;li /&gt;") .attr("id", id) .addClass("{rank:" + rank + "}") var topicName = $("&lt;p /&gt;") .append(name) .addClass("tName"); return li.append(topicName); } }(); </code></pre> <p>Here is a call</p> <pre><code>$("#searchButton").click( function () { Search.GetTopics() }); </code></pre> <p>So Search.GetTopics() must format a list of topics and present them in a div #searchResult.</p> <p>Number of the topics can be around 100.</p> <p>Problem is each search call increases memory usage with 1-3Mb. It happend in IE8 and Firefox.</p> <p>It is a RIA with long-running scripts, so it is important to limit memory usage.</p> <p>Where is the problem? How can I optimize the code, refactoring? Is it smart to use singletones this way?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload