Server-side $resource pagination versus client-side limitTo for large AngularJS datasets
0 reputation · 20 Apr 2023, 05:54 UTC
0 reputation · 20 Apr 2023, 05:54 UTC
Goal: Bound a query and paginate a large dataset in an AngularJS 1.x application, which has no built-in pagination service.
Constraint: AngularJS does not prescribe server-side or client-side paging. Server-side paging through $http or $resource sends page/size or offset/limit parameters, keeping browser memory low but requiring backend endpoints and a network round trip per page. Client-side paging loads the full collection once and applies the limitTo filter, available since AngularJS 1.2, avoiding extra requests but increasing initial payload and digest-scope cost.
Unresolved decision: whether to bound the query at the server before transfer or bound the view after transferring everything. Client-side limitTo on very large arrays can slow digest cycles, while server-side paging requires consistent backend sorting and filtering contracts.
Questions:
34775 reputation · 20 Apr 2023, 09:31 UTC
For a large dataset in AngularJS 1.x, bound the query at the server and return only the requested page. Use client-side limitTo only when the full collection is already small enough to load and digest safely.
Expose the total count from the backend for UI controls via an HTTP header such as X-Total-Count or an envelope body {items:[], total:N} handled with $resource isArray:false and transformResponse. There is no universal row count where client-side digest cost outweighs server round-trip latency; start server-side around a few hundred rows or when payload size and binding complexity make the first load and digest noticeable. Measure in your target browser and network.
Prefer server-side paging for large datasets. Have $resource send page/size or offset/limit and return only that page. This bounds payload and browser memory.
app.factory('Items', ['$resource', function($resource){
return $resource('/api/items', {}, {
page: {
method:'GET',
params:{page:'@page', pageSize:'@pageSize'},
isArray:false
}
});
}]);Use track by a unique id in ng-repeat and keep page size small to reduce DOM and digest cost regardless of strategy.
Return the count that reflects the same filter, sort, page and size parameters as the items.
The count must be consistent with the applied filters; otherwise pagination controls will be incorrect.
No fixed threshold exists. The crossover depends on item size, binding complexity, client CPU, and round-trip time. As a rule of thumb, start server-side around a few hundred rows or when full payload is large relative to network latency.
Verification steps for this case:
Assumption and uncertainty: AngularJS 1.x is end-of-life; behavior is based on well-established 1.x semantics. Exact safe row count cannot be inferred without measurement in your target browser and network conditions. Client-side sorting/filtering before limitTo requires the full dataset in memory and can invalidate pagination if backend data changes between operations.
One missing diagnostic detail that would change the recommendation: approximate row count and average item payload size, plus the complexity of bindings per row in your ng-repeat. With that, a more specific threshold can be estimated.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.