0

Are there any functional or performance differences between the following two blocks?

<foo ng-if="bar['qux']">
<foo ng-if="bar.qux">

Does it make a difference if bar.qux is/isn't initially defined?

Note: I fully understand how objects work in plain Javascript. This is a NOT a question about Javascript, it's about differences that might exist in AngularJS (because of parsing, or any other implementation details)

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Maybe this will answer your question. See the answer of user Alp: https://stackoverflow.com/a/17295727/1735789 – Rafael Gadotti Bachovas Jul 05 '17 at 13:03
  • Thanks @RafaelGadottiBachovas. But as said, I'm not looking for a generic JS interpretation (otherwise I would just go with dot notation). – Diego Mijelshon Jul 05 '17 at 13:05
  • Apparently there is no significative ´performance difference between brackets and dot notation as per https://stackoverflow.com/a/30975165/4488121 but I don't know about angularjs expression parser, it might be an issue. But from the functional side, bracket notation allow you to use properties named with invalid identifier chars like bar['2foo'] that you can't do with dot notation which supports only valid identifiers. – lenilsondc Jul 05 '17 at 13:10
  • Thanks @LenilsondeCastro I know that. It is not what I'm looking for. – Diego Mijelshon Jul 05 '17 at 13:13
  • 1
    @DiegoMijelshon I have added proper performance test for you, it seems that dot notation wins actually. I was curious about this as well. Cheers :{D – lenilsondc Jul 05 '17 at 17:20

2 Answers2

1

I've created a proper jsPerf test case to make sure which notation would be parsed faster and the results are that dot notations are around 6% faster than bracket notation.

JsPerf test case: https://jsperf.com/test-angularjs-parse-speed.

Dot notation is faster, however, performance differences seems to be too little, so I you can choose which of them to use based on what is more readable and practical for you (or your team). Because both run in more than 10,000,000 Operations per second 7% wouldn't cause any performance issue for your app for such simple expression.

lenilsondc
  • 9,590
  • 2
  • 25
  • 40
0

With dot notation, after the initialization the object relationship never changes since the periodic calls.