0

What are the performance implications of running a function within an Angular expression? For example:

<button ng-if="isValid()">Valid</button>

I'm assuming the function isValid() is being run for each digest loop, which occur many times per second. What options are available within Angular for improving the performance here?

One idea would be to run isValid() within a timeout function a few times a second, then setting a scope variable, so I can control the speed of the "digest". Are there other options people use?

11
  • Using function when binding is not a good idea at all, but sometimes it can be necessary. Anyways, How isValid() looks like? Can you show? Commented Oct 21, 2016 at 14:42
  • isValid is a function which returns true or false, and does quite a bit of work - looping through an internal structure and calling many functions. Commented Oct 21, 2016 at 14:44
  • What does the button should do on click? Commented Oct 21, 2016 at 14:46
  • Not relevant to the question. It could be a link or any DOM element for that matter. Commented Oct 21, 2016 at 14:47
  • Hmm, I think it could be, you can move the isValid() inside the button click and prevent the natural action in case it return false. Commented Oct 21, 2016 at 14:50

3 Answers 3

1

I think if function body is just a simple logic comparison, won't affect performance, but if it gets complex, you can try profiling your code via chrome tools to see how much is performing,

Another alternative is also you can try binding the ng-if to a controller scope property instead and modify the value from other part of your controller's code, so maybe you can apply kind of debounce (https://github.com/shahata/angular-debounce) technique to avoid set the scope variable tons of times

Sign up to request clarification or add additional context in comments.

1 Comment

I have said in comments this is a complex function. Debounce is an interesting idea, I will read up on that now.
1

It looks like you're checking if a form is valid before submitting. First of all, your ng-if on the button defeats the purpose of using the same function to conditionally apply a class, since it won't even be in the DOM if isValid() returns false.

You should be able to use ng-pattern, ng-change hooks on your inputs to determine validity as the user is entering form values. Then on your button you can apply the class based on the form's validity:

<button ng-class="{
       'btn-success': myFormName.$valid,
        'btn-danger':  !myFormName.$valid
    }" ng-bind="(myFormName.$valid) ? 'Valid' : 'Invalid'"></button>

If you want to suppress form submission, you can check validity of the form inside a function with ng-submit on the <form> tag:

<form name="myFormName" ng-submit="myFormName.$valid && mySubmitFunction()">

6 Comments

That's not really the point of my question. I'm asking about performance of the function and what options are available. You're getting all hung up on everything superfluous to the actual question.
I'm just trying to point out that you should be checking the validity on an input by input basis, not on every digest.
Ok, +1 inorganik, but I don't really want to assume anything about what's changing the results of isValid() (e.g. changes come from a variety of sources, like timer functions firing, watchers, etc not just form input -- which would be more straightforward case)
Curious how something that has nothing to do with user input can be invalid? If it's invalid, that means the user interacted with it. You should be checking against the specific interactions, not just running an isValid() check periodically.
For example, you can run a long running script that periodically checks the network for anything that makes isValid() change.
|
0

One way is to run the isValid function within an interval function, setting a simple scope variable within. For example:

$interval(function() {
  scope.isvalid = scope.isValid();
}, 100);

This will run every 100ms, but is probably preferable to the number of calls the digest makes. You can also use $watch or $observe to reduce the number of calls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.