$ scope. $ apply slows down performance

I have a single page application using AngularJS and I am facing one performance issue. My app handles inbound server side events that are passed to the client side AngularJS framework using ASP.NET SignalR. There are millions of events that can be received by my application and there are no performance issues on the server side and it easily passes this number of events one by one to the AngularJS framework. The problem lies with the client. After handling the event, I use $ scope. $ Apply () to update the page and display events. In this case, when multiple events are received one after the other, calling $ scope. $ Apply () slows down the application every time and doesn't show events quickly. Events will be relayed at random, so I don't even knowhow any events will be received by my application at any given time.

Any ideas on how to fix this issue would be very helpful.

Thank.

+3


source to share


1 answer


$scope.$apply()

Use instead $scope.$evalAsync()

.

From the docs:



Executes an expression in the current scope at a later point in time.

$evalAsync

makes no guarantees as to when expression

only that:

  • it will run after the function that scheduled the evaluation (preferably before the DOM is rendered).
  • at least one loop $digest

    will be executed after execution expression

    .

Any exceptions from the execution of the expression are sent to $exceptionHandler

service.

Note: if this function is called outside of a loop $digest

, a new Loop $digest

will be scheduled. However, it is always recommended that the model changes from the call $apply

. Which includes the code evaluated through $evalAsync

.

I also had a method $scope.$safeApply()

that was actually a rejected call $scope.$evalAsync()

.

+4


source







All Articles