Skip to content

Performance of scope.$watch #2

@dunksmith

Description

@dunksmith

Using scope.$watch without passing an expression very inefficent, since it fires on any scope change (whether relevent to the plugin or not). Profiling in Chrome showed that this was causing dotdotdot to eat up CPU time.

Maybe you could at least mention this in the readme to avoid people getting caught out?

Here's my workaround - not great as the scope variable is hardcoded, but hopefully you get the general idea.

app.directive('dotdotdot', ['$timeout', function($timeout){
    return {
        restrict: 'A',
        scope: true,    // Pass scope in
        link: function(scope, element, attributes) {

            // Hardcode watch expression (must be a better way??)
            scope.$watch('scopeVarThatAffectsSize', function() {

                // Wait for element to render - 400 appears to be a good number from testing
                $timeout(function() {
                    // Don't use { watch: true } here as it polls continuously
                    element.dotdotdot();
                }, 400);
            });
        }
    }
}]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions