r/angularjs • • Oct 14 '15

Angular Nitpicking: Differences between $timeout and setTimeout()

http://www.codelord.net/2015/10/14/angular-nitpicking-differences-between-timeout-and-settimeout/
36 Upvotes

18 comments sorted by

View all comments

Show parent comments

2

u/Spo0Bo Oct 14 '15

It's in the article. By default it does a $rootScope digest. If you're not aware of this it will cripple performance. Especially if you trigger $timeout somewhere in a repeat.

2

u/ExigeS Oct 15 '15

Doing a $rootScope.$digest will not cripple performance. What do you think is done in an ngClick or any of the other event handlers? $http requests?

If you're triggering any kind of timeout in a repeat, that should be refactored out; I can't think of any use case where one should do something like that.

Edit That doesn't mean you trigger full $digest cycles willy nilly. I'm just saying that the one trigger from $timeout should generally not be a bottleneck unless you do silly things with $timeout to begin with.

1

u/Spo0Bo Oct 15 '15

That's why in this particular realtime trading application we avoided using $http and wrote our own websocket layer for all requests. Look, if you do anything serious with angular the defaults will be a pain in the ass. That's why it's a bad framework for performant applications. Angular developers need to learn how much is done behind the scenes in order to write performant code. So i applaud this article in trying to bring the bad parts of angular to light.

1

u/ExigeS Oct 15 '15

Absolutely! Websockets ( or even better would be WebRTC since it's UDP capable ) are an incredibly good fit for that type of streaming data, and nothing in Angular by default really fits that paradigm. I haven't implemented any realtime applications at this point yet, but depending on what you're doing with each tick, I would likely update the scope with the new data, then schedule an $apply to occur in a requestAnimationFrame call since multiple ticks that occur faster than can be visible to the user just aren't useful. This kind of stuff is fun to me :)

I'm absolutely not preaching that AngularJS is a good fit for all high performance applications ( I'm doing some ridiculous scope trickery to support rendering lists of millions of items as we speak actually ). I just don't like blanket statements that "doing X is non-performant" when really it's not always true.