SStanley
SStanley

Reputation: 740

Angular performance with ng-if="!!someVar" vs ng-if="someVar"

Hi I was just wondering if one can gain better performance using ng-if="!!someVar" where someVar is an object that either is defined or undefined instead of ng-if="someVar". Both clearly work in Angular JS I was wondering what is easier for javascript and angular to evaluate?

In summary, which is the best way to write the ng-if directive when testing for the presence of an object in the current $scope/context.

ng-if="!!someVar or ng-if="someVar"

Upvotes: 2

Views: 717

Answers (2)

Kody
Kody

Reputation: 965

You may see performance gains if you were using purely JavaScript, but since you are already using The AngularJS ng-if Directive, I hardly think you need to worry about a performance difference between ng-if="!!someVar" and ng-if="someVar" to see if that object exists.

I'm sure you are also aware that ng-if is preferred over ng-show, so it's not as though ng-if is not recommended.

Some resources:

Upvotes: 1

RichG
RichG

Reputation: 309

I don't think it really matters that much, but empty objects will still equate to a truthy value in javascript.

!!someVar === true;

Heres a plunk displaying the evaluation: Plnkr here, open the console and run

if you want the object to evaluate to false, use

Object.keys(someVar).length

Upvotes: 1

Related Questions