Angular 2 Performance: Is it better to bind with a data member than a function?

前端 未结 1 1636
梦如初夏
梦如初夏 2020-12-19 07:56

I wanted to have some idea about whether binding to data member is better in performance vs binding to a function?

e.g. which of the below statement will have better

相关标签:
1条回答
  • 2020-12-19 08:41

    If you use the approach *ngIf="isThisTrue" the compiler will generate the following updateRenderer function:

    function (_ck, _v) {
        var _co = _v.component;
        var currVal_1 = _co.isThisTrue;   <--- simple member access
        _ck(_v, 5, 0, currVal_1);
    }
    

    If you use the second approach *ngIf="seeIfItHasBecomeTrue()", the function will look like this:

    function(_ck,_v) {
        var _co = _v.component;
        var currVal_1 = _co.seeIfItHasBecomeTrue();   <--- function call
        _ck(_v,5,0,currVal_1);
    }
    

    And the function call is more performance heavy than simple member access.

    To learn more about updateRenderer function read:

    • The mechanics of DOM updates in Angular
    0 讨论(0)
提交回复
热议问题