angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

$http should coalesce calls to $apply

Open kseamon opened this issue 12 years ago • 30 comments

Presently, $http calls $apply for every request response, even if they come in within milliseconds of each other. Using a short timeout (0ms, which in practice works out to 0-10ms) to $apply can reduce a lot of waste.

See example implementation here: https://github.com/kseamon/angular.js/compare/http-coalesce?expand=1

Using this code (Not ready for a pull request as it breaks tests and mocks, etc), I see the number of $apply calls in my app's startup drop from 40 to 16.

kseamon avatar Dec 05 '13 20:12 kseamon

@cmelion from HBO is also interested in a solution like this.

IgorMinar avatar Dec 06 '13 21:12 IgorMinar

The PR I'm working on introduces an $$applyAsync function to $rootScope. Calling it results in a timeout that will call apply. Repeated calls of it are coalesced into a single $apply.

For starters, I'm calling it from $http, $evalAsync, and $cookies.

kseamon avatar Dec 06 '13 22:12 kseamon

I'll probably have it use a 0ms timeout. Increasing it does result in fewer apply calls in practice, but in my app, there's not much value to going above 10ms. I'd be fine using that too, but at some point we're slowing things down more than speeding them up.

kseamon avatar Dec 06 '13 22:12 kseamon

+1

timkindberg avatar Dec 14 '13 20:12 timkindberg

+1

kadimisetty avatar Dec 14 '13 21:12 kadimisetty

+1

dtabuenc avatar Dec 14 '13 21:12 dtabuenc

+1

fwielstra avatar Dec 14 '13 22:12 fwielstra

+1

cesarandreu avatar Dec 14 '13 23:12 cesarandreu

+1

pixelcort avatar Dec 15 '13 07:12 pixelcort

+1

erwinmombay avatar Dec 15 '13 07:12 erwinmombay

+1

damien-quintal avatar Dec 15 '13 08:12 damien-quintal

+1

codef0rmer avatar Dec 15 '13 17:12 codef0rmer

+1

PatrickJS avatar Dec 15 '13 18:12 PatrickJS

+1

just-boris avatar Dec 16 '13 08:12 just-boris

+1

Fjandin avatar Dec 16 '13 13:12 Fjandin

+1

timelf123 avatar Dec 16 '13 15:12 timelf123

+1

MattWalker avatar Dec 16 '13 19:12 MattWalker

+1

greglockwood avatar Dec 17 '13 22:12 greglockwood

@kseamon since this proposal was already approved for 1.3 you don't need to prefix it with $$ use $ in your PR instead.

IgorMinar avatar Dec 17 '13 23:12 IgorMinar

+1

rynz avatar Dec 18 '13 06:12 rynz