Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:29:47+00:00 2026-06-17T11:29:47+00:00

I have a single-page app where the user pages through lists of items. Each

  • 0

I have a single-page app where the user pages through lists of items. Each item, in turn, has a list of items.

An observable array is updated with new items from the server retrieved via an AJAX request. This all works fine.

Unfortunately after a few pages, the number of operations performed (and the amount of memory used in browsers like FireFox and IE8) keeps going up. I’ve tracked it down to the fact that elements in my observable array are not being cleaned up properly and are actually still in memory, even though I’ve replaced the items in my observable array with new data.

I’ve created a small example that replicates the problem I’m seeing:

HTML:

<p data-bind="text: timesComputed"></p>
<button data-bind="click: more">MORE</button>
<ul data-bind="template: { name: 'items-template', foreach: items }">
</ul>

<script id="items-template">
    <li>
        <p data-bind="text: text"></p>
        <ul data-bind="template: { name: 'subitems-template', foreach: subItems }"></ul>
    </li>
</script>

<script id="subitems-template">
    <li>
        <p data-bind="text: text"></p>
    </li>
</script>

JavaScript/KnockoutJS ViewModels:

var subItemIndex = 0;

$("#clear").on("click", function () {
  $("#log").empty();
});

function log(msg) {
  $("#log").text(function (_, current) {
    return current + "\n" + msg;
  });
}
function Item(num, root) {
  var idx = 0;

  this.text = ko.observable("Item " + num);
  this.subItems = ko.observableArray([]);
  this.addSubItem = function () {
    this.subItems.push(new SubItem(++subItemIndex, root));
  }.bind(this);

  this.addSubItem();
  this.addSubItem();
  this.addSubItem();
}

function SubItem(num, root) {
  this.text = ko.observable("SubItem " + num);
  this.computed = ko.computed(function () {
    log("computing for " + this.text());
    return root.text();
  }, this);

  this.computed.subscribe(function () {
    root.timesComputed(root.timesComputed() + 1);
  }, this);
}

function Root() {
  var i = 0;

  this.items = ko.observableArray([]);
  this.addItem = function () {
    this.items.push(new Item(++i, this));
  }.bind(this);

  this.text = ko.observable("More clicked: ");
  this.timesComputed = ko.observable(0);

  this.more = function () {
    this.items.removeAll();
    this.addItem();
    this.addItem();
    this.addItem();    
    this.timesComputed(0);
    this.text("More clicked " + i);
  }.bind(this);

  this.more();
}

var vm = new Root();

ko.applyBindings(vm);

If you look at the fiddle, you will notice that the “log” contains an entry for every single ViewModel ever created. the computed property SubItem.computed is run even after I expected each of those items to be long gone. This is causing a serious degradation in performance in my application.

So my questions are:

  • What am I doing wrong here? Am I expecting KnockoutJS to dispose of ViewModels that I actually need to be disposing of manually?
  • Is my use of ko.computed on SubItem causing the issue?
  • If KnockoutJS is not going to dispose of these viewmodels, how should I be disposing of them myself?

Update: After some further digging, I’m pretty sure the computed property in SubItem is the culprit. However, I still don’t understand why that property is still being evaluated. Shouldn’t SubItem be destroyed when the observable array is updated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:29:48+00:00Added an answer on June 17, 2026 at 11:29 am

    The JavaScript garbage collector can only dispose a computed observable once all references to it and its dependencies are dropped. That’s because observables keep a reference to any computed observables that depend on them (and vice versa).

    One solution is to make the computed observable dispose itself when it no longer has any dependencies. This can be done easily using a helper function like this.

    function autoDisposeComputed(readFunc) {
        var computed = ko.computed({
            read: readFunc,
            deferEvaluation: true,
            disposeWhen: function() {
                return !computed.getSubscriptionsCount();
            }
        });
        return computed;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single page app which has scrolling DIVs as part of the
i have a web application that is build as single page. Through one user
I have A single page that has the following structure <form runat=server><placeholder></placeholder></form> I have
I have a single page website that changes content based on variables passed through
Basically I have a single page site that scrolls when the user clicks on
I have a nested user control which appears on every single page. It contains
I have multiple pages being pulled together into a single page. Some of these
I have an ASP.NET web app that uses a single page, but makes some
Background: I'm working on a single page web app that loads everything through AJAX
I have a single-page app that uses a lot of CSS3 shadows. The app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.