X Tutup
Skip to content

Commit 761c6d0

Browse files
committed
fix(perf): faster looseIdentical implementation
Remove String type check in looseIdentical in JS-mode. It is not necessary as dart2js already compiles `identical` to `===` which compares string contents. Inline call sites. This improves change detection of plain fields by 40%. On a large internal app the improvement is 5%. Closes #6364
1 parent 3e65d14 commit 761c6d0

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

modules/angular2/src/facade/lang.dart

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,26 @@ class FunctionWrapper {
235235

236236
const _NAN_KEY = const Object();
237237

238-
// Dart can have identical(str1, str2) == false while str1 == str2. Moreover,
239-
// after compiling with dart2js identical(str1, str2) might return true.
240-
// (see dartbug.com/22496 for details).
241-
bool looseIdentical(a, b) =>
238+
// Dart VM implements `identical` as true reference identity. JavaScript does
239+
// not have this. The closest we have in JS is `===`. However, for strings JS
240+
// would actually compare the contents rather than references. `dart2js`
241+
// compiles `identical` to `===` and therefore there is a discrepancy between
242+
// Dart VM and `dart2js`. The implementation of `looseIdentical` attempts to
243+
// bridge the gap between the two while retaining good performance
244+
// characteristics. In JS we use simple `identical`, which compiles to `===`,
245+
// and in Dart VM we emulate the semantics of `===` by special-casing strings.
246+
// Note that the VM check is a compile-time constant. This allows `dart2js` to
247+
// evaluate the conditional during compilation and inline the entire function.
248+
//
249+
// See: dartbug.com/22496, dartbug.com/25270
250+
const _IS_DART_VM = !identical(1.0, 1); // a hack
251+
bool looseIdentical(a, b) => _IS_DART_VM
252+
? _looseIdentical(a, b)
253+
: identical(a, b);
254+
255+
// This function is intentionally separated from `looseIdentical` to keep the
256+
// number of AST nodes low enough for `dart2js` to inline the code.
257+
bool _looseIdentical(a, b) =>
242258
a is String && b is String ? a == b : identical(a, b);
243259

244260
// Dart compare map keys by equality and we can have NaN != NaN
@@ -334,4 +350,4 @@ var global = null;
334350

335351
dynamic evalExpression(String sourceUrl, String expr, String declarations, Map<String, String> vars) {
336352
throw "Dart does not support evaluating expression during runtime!";
337-
}
353+
}

0 commit comments

Comments
 (0)
X Tutup