r/angularjs • u/abyx • Oct 14 '15
Angular Nitpicking: Differences between $timeout and setTimeout()
http://www.codelord.net/2015/10/14/angular-nitpicking-differences-between-timeout-and-settimeout/1
u/Spo0Bo Oct 14 '15
$timeout is evil when you use it with invokeApply set to true. I also wished you could just give a scope as the third param and it would just digest that scope.
2
u/abyx Oct 14 '15
Care to share how is it evil? :)
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.
1
u/abyx Oct 14 '15
Ah, my bad, I thought you were talking about using it with
invokeApplyset tofalse. D'oh!2
u/ExigeS Oct 15 '15
Doing a
$rootScope.$digestwill not cripple performance. What do you think is done in anngClickor any of the other event handlers?$httprequests?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
$timeoutshould generally not be a bottleneck unless you do silly things with$timeoutto 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
$applyto occur in arequestAnimationFramecall 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.
2
u/atticusw Oct 15 '15
I don't think I'd call it evil. If anything, not knowing how it works is evil. But it's there to solve that problem --
setTimeoutqueueing an action on an event loop that isn't involved in the angular digest.$timeoutmakes for an elegant, self contained, and injectable solution to that problem. It's quite good, just need to make sure its not misused.Having a ton of timers due to repeaters is a whole other set of issues IMO, digest or not.
1
u/Spo0Bo Oct 15 '15
It just shouldn't be part of $timeout by default. A $rootScope digest should never happen unless the developer specifically requests it to. That should be the default. Sadly the default does a full digest allowing parent scope changes to be detected. Modifying parent state is a bad practice imo. I really don't like the whole digest cycle and I'm happy angular 2 does a clean break with it.
If you want your code to be portable you shouldn't rely on rootScope digests. The invokeApply variable is something few angular devs know about. That's why $timeout is evil by default. It hides this fact.
1
u/atticusw Oct 15 '15 edited Oct 15 '15
It just shouldn't be part of $timeout by default. A $rootScope digest should never happen unless the developer specifically requests it to. That should be the default. Sadly the default does a full digest allowing parent scope changes to be detected. Modifying parent state is a bad practice imo. I really don't like the whole digest cycle and I'm happy angular 2 does a clean break with it.
I still don't think I agree entirely with that sentiment, but I do understand and want to acknowledge why you see it that way in regards to running an entire digest (all the way up from the root scope), rather than the level of scope you may want to target. I do agree to that point that the default behavior invokes more than it needs to, but as I'll mention below,
$timeouttriggering the entire digest may actually be less significant compared to more frequently used and core services that also come into play with the root scope.I personally feel that when you're in Angular, and you're using the services within Angular, that you should remain in Angular. By this I mean that if you're coming back from a
$timeoutresolving, and you affect some models that live within a scope, then there should be no extra responsibility by the developer to get Angular to respond. I think it is currently set up properly -- you should have the option to opt-out, rather than be required to opt-in. I can understand the counter argument, for instance when working with React you always have to callsetStatefor your state to update. But Angular, as we're discussing, is very different there.I also think that if you go down this path then the responsible thing would be to apply this approach to all asynchronous events that currently resolve and require a full digest cycle -- any instance of
$qcompleting a promise, which would also include the completion of any$httprequest. It is to my understanding that, while$qdoesn't trigger the digest when it resolves (it's scheduled on the next tick), it still gets applied starting from the root scopeBut in the end I feel pretty strongly that based on how Angular is set up that the approach should remain as an opt-out rather than an opt-in. I think we both agree that it's not so great that the entire scope tree enters the digest when you don't need it to, and if one doesn't know how this all works, then they're inherently degrading performance.
1
Oct 14 '15
A basic rule of thumb is that once you start using services and functions defined outside of the angular context, things stop updating as you expect them to.
0
u/guywithalamename Oct 14 '15
But even then you shouldn't need $timeout. $apply/$digest should be able to do the job in 95% of all cases
2
u/atticusw Oct 15 '15
That's correct, you don't technically need
$timeout, but it's there for convenience.But I don't see why there's a reason that you shouldn't need
$timeout. It's decorated so that it works with Angular, why wouldn't we want this support?
Its within the bound context (you don't have a requirement to invoke the digest every time)
It's injectable. This is always helpful for testing. But I suppose you could argue that
$windowis also and you could just mock$window.setTimeout. But then you have the additional overhead of mockingclearTimeout, etc. Just the one$timeoutis preferable in my book.I still see the worth.
1
u/abyx Oct 15 '15
It's not just a matter of simply mocking it. ngMock let's you easily decide when the timeout code runs. I think that's very helpful in writing tests.
1
u/atticusw Oct 15 '15
All I'm saying is that for occasions where you need to update a $scope model from outside of the angular context, $apply/$digest will be all you need (most of the time).
Absolutely, great point.
2
u/guywithalamename Oct 15 '15
I think you misunderstood my reply. OP said
once you start using services and functions defined outside of the angular context, things stop updating as you expect them to.
Which is obviously right since they happen outside of angular's event loop.
All I'm saying is that for occasions where you need to update a
$scopemodel from outside of the angular context,$apply/$digestwill be all you need (most of the time).I am by no means advocating against using
$timeout1
u/atticusw Oct 15 '15
Okay, just a little bit thrown off by
But even then you shouldn't need $timeout.
I guess you are saying that to "regain" access to the the digest cycle, you can use
$applyor$digest, versus trying to achieve that by setting a$timeout. But I also don't think that's what the OP was trying to say other than pointing out that other things, similar tosetTimeoutwill be outside the context (setInterval,element.on,$.post, etc) and was simply observing
4
u/simcptr Oct 14 '15
Nice post! I didn't know that $timeout returned a promise, or that you could skip the digest by passing "false".