It is impossible to understand why disposing of computed observables does not remove subscriptions from global variables in the case view model created using the knockout.mapping plugin.
First, let's see what happens when a model is created directly:
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <button data-bind="click: addItems">Add items</button> <button data-bind="click: removeItems">Remove items</button> <div data-bind="foreach: items"> <div> <span data-bind="text: price"></span> </div> </div>
I used Chrome dev tools to record heap distributions while adding and removing items. After each addition, previously selected objects were successfully cleaned, I got the following image:
Now the same functionality using the mapping plugin:
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script> <button data-bind="click: addItems">Add items</button> <button data-bind="click: removeItems">Remove items</button> <div data-bind="foreach: items"> <div> <span data-bind="text: price"></span> </div> </div>
Using the same method to write heap distributions, this is what I see:
I know pureComputed , but would like to avoid using them for two reasons:
- Switching to clean computed interrupt code for legacy code by throwing exceptions:
"Computed" net "should not be called recursively
Solving these problems will take a long time.
- Clean calculations are calculated more often, which creates some performance overhead that I would like to avoid, and again, this will unpredictably affect legacy code.
I would also like to use the mapping plugin because of its ability to control the state of the collection (using the key
display property) and because it creates all observables for me.
So, is there something I missed and what is the right way to free up resources if using a map plugin?
source share