Skip to content

Instantly share code, notes, and snippets.

@rkgarg
Last active June 28, 2017 11:41
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save rkgarg/7232175 to your computer and use it in GitHub Desktop.
Save rkgarg/7232175 to your computer and use it in GitHub Desktop.
Angular ng-repeat Benchmark
// Post repeat directive for logging the rendering time
angular.module('myApp').directive('postRepeatDirective',
['$timeout',
function($timeout) {
return function(scope) {
if (scope.$first)
window.a = new Date(); // window.a can be updated anywhere if to reset counter at some action if ng-repeat is not getting started from $first
if (scope.$last)
$timeout(function(){
console.log("## DOM rendering list took: " + (new Date() - window.a) + " ms");
});
};
}
]);
// Use in HTML:
<tr ng-repeat="item in items" post-repeat-directive></tr>
@nmccready
Copy link

@prettycode
Copy link

By using $timeout(), isn't it possible there's a whole lot of processing that has nothing to do with the ng-repeat, that executes before the $timeout() does, thereby making it seem like the ng-repeat takes longer than it does?

For example, between when the $timeout() is created in if (scope.$last) and when the $timeout() actually executes the console.log(), what if some other directive writes 50,000 DOM objects?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment