Pure calculation monitoring (pure computed observables)

Source: Internet
Author: User

Pure computational monitoring, available in knockout 3.2.0, provides better management of performance and memory. This is because pure compute monitoring does not contain subscriptions that depend on him. Features include:

    • Prevent memory leaks
    • Reduced computational overhead: The value is no longer observed and is a computed observables that will not be recalculated.

The pure computed observable automatically switches between two states depending on whether a subscription is available:

1. When there is no subscriber (subscribers), it is sleeping status. When you enter the sleeping state, all dependent subscriptions are released. During this state, it will not subscribe to any of the observable attributes that are used in the function (but by getdependenciescount () or the resulting value is included). If the value of computed observable is read during sleeping, it will be recalculated immediately.

2. When there is a Subscriber (subscriber), it becomes listening. When entering the listening state, the calculation function should be called immediately. In this state, it is the same as the normal computed observable. Refer to how dependency tracking works.

Why use "pure"?

Let's take a look at the introduction of "pure functions" introduced online:

Pure functions Exchange data with the outside world with only the unique channels-parameters and return values. There is no logical side effect (the side effect is the potential to exchange data with the external environment of the function)-which is, of course, from an abstract point of view. Realistically, any function has side effects at the CPU level, and most functions have side effects at the heap level. But even with these side effects, this abstraction at the logical level is meaningful.

Pure functions do not read and write global variables, stateless, no IO, do not change any parameters passed in. Ideally, it would not give him any external data, because once you pass in a pointer such as Allmyglobals, the rules above are broken.

The pure functions is borrowed here because of their special looks:

1. Performing computed observable does not produce any side effects.

2. The value of computed observable does not depend on other hidden information, its value should be based on other observables, and these values can be considered as parameters that define the computed observable function.

Grammar

The standard syntax is to use ko.purecomputed:

this. FullName = ko.purecomputed (function() {    returnthis this this);  

Alternatively, you can use the ko.computed option:

this. FullName = ko.computed (function() {    return this This  thistrue });

For more syntax instructions, see computed observable reference.

When to use pure computed observable

The use of pure computed observables in a durable view model can improve performance (but not always) when the design of the program includes a durable view model that is used by other temporary views and viewmodels. Use in temporary view models optimizes memory. (not yet feeling the benefits of these two points).

The following example is an example of a simple wizard, enter first name, enter last name, and finally the full name:

(because full name does not appear at the beginning, full name is not called when it enters its dependencies, and is only called when full name is displayed.) Do not map, very troublesome, directly put HTML and JS into an HTML can be run, or see knockout website http://knockoutjs.com/documentation/ Computed-pure.html, but here the interface and HTML are not, but the function is the same):

<Divclass= "Log"Data-bind= "Text:computedlog"></Div><!--ko if:step () = = 0 -    <P>First Name:<inputData-bind= "Textinput:firstname" /></P><!--/ko -<!--ko if:step () = = 1 -    <P>Last Name:<inputData-bind= "Textinput:lastname" /></P><!--/ko -<!--ko if:step () = = 2 -    <Div>Prefix:<SelectData-bind= "Value:prefix, options: [' Mr. ', ' Ms. ', ' Mrs. ', ' Dr. ']"></Select></Div>    <H2>Hello,<spanData-bind= "Text:fullname"> </span>!</H2><!--/ko -<P><Buttontype= "button"Data-bind= "Click:next">Next</Button></P>
functionAppData () { This. FirstName = ko.observable (' John ');  This. LastName = ko.observable (' Burns ');  This. prefix = ko.observable (' Dr. ');  This. Computedlog = ko.observable (' Log: ');  This. FullName = ko.purecomputed (function () {        varValue = This. prefix () + "" + This. FirstName () + "" + This. LastName (); //Normally, should avoid writing to observables within a pure computed        //observable (avoiding side effects). But this example was meant to demonstrate        //Its internal workings, and writing a log is a good a-to-do.         This. Computedlog ( This. Computedlog.peek () + value + '; ‘); returnvalue; },  This);  This. Step = ko.observable (0);  This. Next =function () {         This. Step ( This. Step () = = 2? 0: This. Step () +1); };}; Ko.applybindings (NewAppData ());

(You can try to change the Computedlog.peek () to Computedlog (), and the runtime prompts you not to loop. But change the purecomputed to computed, and then use Computedlog () will not have this hint. I don't know why. )

When not to use pure computed observable

Side effects

When a dependent property changes, you need to do something right away, such as:

To invoke the callback function in computed observable

Ko.computed (function  () {    var cleandata = Ko.tojs (this);     this);

In the bound init function, update the binding element with computed observable

ko.computed ({    function  () {        = ko.unwrap (valueaccessor ());    },    disposewhennodeisremoved:element});

One obvious side effect of using pure computed is that the calculation function is not called when the value of the Subscriber changes (because it is sleeping). When the value of the dependency changes when the calculation function must be called, in which case the pure computed should not be used, and the regular computed should be used.

Performance

Using pure computed observable can also cause significant performance overhead, and normal computed observable will be triggered when the dependent value changes, but pure computed observable will be evaluated once per visit. This may lead to excessive performance overhead in some cases.

Pure calculation monitoring (pure computed observables)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.