@@ -891,19 +891,19 @@ function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp, options) {
891
891
? function cspSafeGetter ( scope , locals ) {
892
892
var pathVal = ( locals && locals . hasOwnProperty ( key0 ) ) ? locals : scope ;
893
893
894
- if ( pathVal === null || pathVal === undefined ) return pathVal ;
894
+ if ( pathVal == null ) return pathVal ;
895
895
pathVal = pathVal [ key0 ] ;
896
896
897
- if ( ! key1 || pathVal === null || pathVal === undefined ) return pathVal ;
897
+ if ( ! key1 || pathVal == null ) return pathVal ;
898
898
pathVal = pathVal [ key1 ] ;
899
899
900
- if ( ! key2 || pathVal === null || pathVal === undefined ) return pathVal ;
900
+ if ( ! key2 || pathVal == null ) return pathVal ;
901
901
pathVal = pathVal [ key2 ] ;
902
902
903
- if ( ! key3 || pathVal === null || pathVal === undefined ) return pathVal ;
903
+ if ( ! key3 || pathVal == null ) return pathVal ;
904
904
pathVal = pathVal [ key3 ] ;
905
905
906
- if ( ! key4 || pathVal === null || pathVal === undefined ) return pathVal ;
906
+ if ( ! key4 || pathVal == null ) return pathVal ;
907
907
pathVal = pathVal [ key4 ] ;
908
908
909
909
return pathVal ;
@@ -912,7 +912,7 @@ function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp, options) {
912
912
var pathVal = ( locals && locals . hasOwnProperty ( key0 ) ) ? locals : scope ,
913
913
promise ;
914
914
915
- if ( pathVal === null || pathVal === undefined ) return pathVal ;
915
+ if ( pathVal == null ) return pathVal ;
916
916
917
917
pathVal = pathVal [ key0 ] ;
918
918
if ( pathVal && pathVal . then ) {
@@ -924,7 +924,7 @@ function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp, options) {
924
924
}
925
925
pathVal = pathVal . $$v ;
926
926
}
927
- if ( ! key1 || pathVal === null || pathVal === undefined ) return pathVal ;
927
+ if ( ! key1 || pathVal == null ) return pathVal ;
928
928
929
929
pathVal = pathVal [ key1 ] ;
930
930
if ( pathVal && pathVal . then ) {
@@ -936,7 +936,7 @@ function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp, options) {
936
936
}
937
937
pathVal = pathVal . $$v ;
938
938
}
939
- if ( ! key2 || pathVal === null || pathVal === undefined ) return pathVal ;
939
+ if ( ! key2 || pathVal == null ) return pathVal ;
940
940
941
941
pathVal = pathVal [ key2 ] ;
942
942
if ( pathVal && pathVal . then ) {
@@ -948,7 +948,7 @@ function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp, options) {
948
948
}
949
949
pathVal = pathVal . $$v ;
950
950
}
951
- if ( ! key3 || pathVal === null || pathVal === undefined ) return pathVal ;
951
+ if ( ! key3 || pathVal == null ) return pathVal ;
952
952
953
953
pathVal = pathVal [ key3 ] ;
954
954
if ( pathVal && pathVal . then ) {
@@ -960,7 +960,7 @@ function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp, options) {
960
960
}
961
961
pathVal = pathVal . $$v ;
962
962
}
963
- if ( ! key4 || pathVal === null || pathVal === undefined ) return pathVal ;
963
+ if ( ! key4 || pathVal == null ) return pathVal ;
964
964
965
965
pathVal = pathVal [ key4 ] ;
966
966
if ( pathVal && pathVal . then ) {
@@ -976,6 +976,27 @@ function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp, options) {
976
976
} ;
977
977
}
978
978
979
+ function simpleGetterFn1 ( key0 , fullExp ) {
980
+ ensureSafeMemberName ( key0 , fullExp ) ;
981
+
982
+ return function simpleGetterFn1 ( scope , locals ) {
983
+ if ( scope == null ) return scope ;
984
+ return ( ( locals && locals . hasOwnProperty ( key0 ) ) ? locals : scope ) [ key0 ] ;
985
+ } ;
986
+ }
987
+
988
+ function simpleGetterFn2 ( key0 , key1 , fullExp ) {
989
+ ensureSafeMemberName ( key0 , fullExp ) ;
990
+ ensureSafeMemberName ( key1 , fullExp ) ;
991
+
992
+ return function simpleGetterFn2 ( scope , locals ) {
993
+ if ( scope == null ) return scope ;
994
+ scope = ( ( locals && locals . hasOwnProperty ( key0 ) ) ? locals : scope ) [ key0 ] ;
995
+
996
+ return scope == null ? scope : scope [ key1 ] ;
997
+ } ;
998
+ }
999
+
979
1000
function getterFn ( path , options , fullExp ) {
980
1001
// Check whether the cache has this getter already.
981
1002
// We can use hasOwnProperty directly on the cache because we ensure,
@@ -988,7 +1009,13 @@ function getterFn(path, options, fullExp) {
988
1009
pathKeysLength = pathKeys . length ,
989
1010
fn ;
990
1011
991
- if ( options . csp ) {
1012
+ // When we have only 1 or 2 tokens, use optimized special case closures.
1013
+ // http://jsperf.com/angularjs-parse-getter/6
1014
+ if ( ! options . unwrapPromises && pathKeysLength === 1 ) {
1015
+ fn = simpleGetterFn1 ( pathKeys [ 0 ] , fullExp ) ;
1016
+ } else if ( ! options . unwrapPromises && pathKeysLength === 2 ) {
1017
+ fn = simpleGetterFn2 ( pathKeys [ 0 ] , pathKeys [ 1 ] , fullExp ) ;
1018
+ } else if ( options . csp ) {
992
1019
if ( pathKeysLength < 6 ) {
993
1020
fn = cspSafeGetterFn ( pathKeys [ 0 ] , pathKeys [ 1 ] , pathKeys [ 2 ] , pathKeys [ 3 ] , pathKeys [ 4 ] , fullExp ,
994
1021
options ) ;
@@ -1006,11 +1033,10 @@ function getterFn(path, options, fullExp) {
1006
1033
} ;
1007
1034
}
1008
1035
} else {
1009
- var code = 'var l, fn, p;\n' ;
1036
+ var code = 'var p;\n' ;
1010
1037
forEach ( pathKeys , function ( key , index ) {
1011
1038
ensureSafeMemberName ( key , fullExp ) ;
1012
- code += 'if(s === null || s === undefined) return s;\n' +
1013
- 'l=s;\n' +
1039
+ code += 'if(s == null) return s;\n' +
1014
1040
's=' + ( index
1015
1041
// we simply dereference 's' on any .dot notation
1016
1042
? 's'
@@ -1033,10 +1059,10 @@ function getterFn(path, options, fullExp) {
1033
1059
/* jshint -W054 */
1034
1060
var evaledFnGetter = new Function ( 's' , 'k' , 'pw' , code ) ; // s=scope, k=locals, pw=promiseWarning
1035
1061
/* jshint +W054 */
1036
- evaledFnGetter . toString = function ( ) { return code ; } ;
1037
- fn = function ( scope , locals ) {
1062
+ evaledFnGetter . toString = valueFn ( code ) ;
1063
+ fn = options . unwrapPromises ? function ( scope , locals ) {
1038
1064
return evaledFnGetter ( scope , locals , promiseWarning ) ;
1039
- } ;
1065
+ } : evaledFnGetter ;
1040
1066
}
1041
1067
1042
1068
// Only cache the value if it's not going to mess up the cache object
0 commit comments