CodexBloom - Programming Q&A Platform

AngularJS 1.8: How to efficiently handle large datasets in ng-repeat with pagination

πŸ‘€ Views: 75 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-12
angularjs ng-repeat pagination performance javascript

Quick question that's been bugging me - I'm currently developing an AngularJS application using version 1.8 and I'm working with performance optimization while rendering a large dataset in an `ng-repeat`. The dataset can contain over 10,000 records, and when I try to display it directly, the UI becomes unresponsive. I attempted to implement pagination by slicing the data array, but I'm still noticing a delay, especially when scrolling through the pages. Here’s the approach I've taken so far: 1. I have a controller that fetches the data from an API and stores it in a scope variable `vm.data`. 2. I'm using a simple pagination logic with `ng-repeat` like this: ```javascript $scope.currentPage = 1; $scope.itemsPerPage = 100; $scope.paginatedData = function() { const start = ($scope.currentPage - 1) * $scope.itemsPerPage; return $scope.data.slice(start, start + $scope.itemsPerPage); }; ``` And in the view: ```html <div ng-repeat="item in paginatedData()"> <div>{{ item.name }}</div> </div> <button ng-click="currentPage = currentPage + 1">Next Page</button> ``` However, clicking the 'Next Page' button results in a noticeable lag before the new items render. The browser console does not show any obvious errors, but I suspect that the data binding in `ng-repeat` is causing performance bottlenecks as the dataset grows. I've also tried using `track by` in the `ng-repeat` to help Angular track changes, but it hasn't made much of a difference: ```html <div ng-repeat="item in paginatedData() track by item.id"> <div>{{ item.name }}</div> </div> ``` I’m wondering if there are best practices or design patterns that I could apply to optimize the rendering of such a large dataset. Is there a more efficient way to implement pagination or virtual scrolling in AngularJS to improve the performance? Any help would be greatly appreciated! Is this even possible? This is for a CLI tool running on macOS. Any help would be greatly appreciated!