Skip to content

Commit

Permalink
Replace a use of putIfAbsent with ??= (dart-lang/collection#125)
Browse files Browse the repository at this point in the history
Because it avoids using a closure, it seems that ??= [] can be used with better performance than putIfAbsent in groupBy.
  • Loading branch information
whesse authored Mar 18, 2020
1 parent 80e5149 commit 5826da6
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions pkgs/collection/lib/src/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Map<K, V> mergeMaps<K, V>(Map<K, V> map1, Map<K, V> map2,
Map<T, List<S>> groupBy<S, T>(Iterable<S> values, T Function(S) key) {
var map = <T, List<S>>{};
for (var element in values) {
var list = map.putIfAbsent(key(element), () => []);
list.add(element);
(map[key(element)] ??= []).add(element);
}
return map;
}
Expand Down

0 comments on commit 5826da6

Please sign in to comment.