Skip to content

Commit 9ab5a18

Browse files
authored
add a basic benchmark for DeepCollectionEquality (#717)
I wanted to check the performance impact of using `.entries` instead of `.keys` followed by a lookup, so I wrote this. Will send out that PR separately - spoiler alert we might not want to land it 🤣 .
1 parent 9e37915 commit 9ab5a18

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

pkgs/collection/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ build/
88
packages
99
.packages
1010
pubspec.lock
11+
benchmark/*.exe
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:benchmark_harness/benchmark_harness.dart';
6+
import 'package:collection/collection.dart';
7+
8+
void main() {
9+
for (var unordered in [true, false]) {
10+
DeepCollectionEqualityEqualsBenchmark(unordered).report();
11+
DeepCollectionEqualityHashBenchmark(unordered).report();
12+
}
13+
}
14+
15+
class DeepCollectionEqualityBase extends BenchmarkBase {
16+
final DeepCollectionEquality equality;
17+
18+
DeepCollectionEqualityBase(bool unordered, String function)
19+
: equality = unordered
20+
? const DeepCollectionEquality.unordered()
21+
: const DeepCollectionEquality(),
22+
super('DeepCollectionQuality${unordered ? 'Unordered' : ''}.$function');
23+
}
24+
25+
class DeepCollectionEqualityHashBenchmark extends DeepCollectionEqualityBase {
26+
DeepCollectionEqualityHashBenchmark(bool unordered)
27+
: super(unordered, 'hash');
28+
29+
@override
30+
void run() {
31+
hash = equality.hash(mapA);
32+
}
33+
34+
static int hash = 0;
35+
}
36+
37+
class DeepCollectionEqualityEqualsBenchmark extends DeepCollectionEqualityBase {
38+
DeepCollectionEqualityEqualsBenchmark(bool unordered)
39+
: super(unordered, 'equals');
40+
41+
@override
42+
void run() {
43+
equals = equality.equals(mapA, mapB);
44+
}
45+
46+
static bool equals = false;
47+
}
48+
49+
final mapA = {
50+
for (var i = 0; i < 100; i++)
51+
{
52+
[
53+
for (var j = i; j < i + 10; j++) j,
54+
]: i.isEven ? i : '$i',
55+
}
56+
};
57+
58+
final mapB = {
59+
for (var i = 0; i < 100; i++)
60+
{
61+
[
62+
for (var j = i; j < i + 10; j++) j,
63+
]: i.isEven ? i : '$i',
64+
}
65+
};

pkgs/collection/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ environment:
1212
sdk: ^3.4.0
1313

1414
dev_dependencies:
15+
benchmark_harness: ^2.3.1
1516
dart_flutter_team_lints: ^3.0.0
1617
test: ^1.16.6

0 commit comments

Comments
 (0)