Charles Sounder
Charles Sounder

Reputation: 591

Which is faster, a directive or one-time binding in AngularJS?

I have an array I want to print on a webpage. The array doesn't change so I don't want Angular watching it's value. W/that in mind, I ask the following question ...

Is it faster to use a directive to grab a scope variable and create an HTML element using this variable as a hard-coded string within the HTMl element

[   e.g. el.html('<div>' + $scope.value + '</div>')   ]

, or ... use a template that has a one time data binding w/:: syntax

[   e.g. {{::value}}   ]?

Upvotes: 0

Views: 413

Answers (2)

Joel Jeske
Joel Jeske

Reputation: 1647

I would strongly suggest to use the angular one time binding as opposed to creating the elements manually. If you are worried about the overhead of a one-time binding, it may be best to use a different framework. A major goal of angular is to minimize direct DOM manipulation.

Upvotes: 1

wjohnsto
wjohnsto

Reputation: 4463

The difference between the two methods is almost certainly trivial unless you're dealing with a VERY large array. My suggestion would be to go with what is cleaner, and in this case it looks like the one-time data binding is going to be the cleanest implementation (and arguably faster).

Upvotes: 0

Related Questions