aboutsummaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorFabian Lange <lange.fabian@gmail.com>2015-03-02 12:16:19 +0100
committerArtur Signell <artur@vaadin.com>2015-03-02 21:51:33 +0200
commit05236b2c8d1908685ddbd0e71d2142202ad3bb09 (patch)
tree9ea949354f5ef5e50a435a6b41f5d8aa6736269a /client
parentea9b7e0508e5c9e96edcd2f72699af29b705e361 (diff)
downloadvaadin-framework-05236b2c8d1908685ddbd0e71d2142202ad3bb09.tar.gz
vaadin-framework-05236b2c8d1908685ddbd0e71d2142202ad3bb09.zip
Improve performance of Util.collectionEquals (#16968).
This change checks the length of both collections first before iterating them. This massively speeds up the comparison in case the collections do not have the same length. Also in the case where the lengths are equal, this saves the collection2.hasNext() checks. Change-Id: If1325f3581d12bf742d77fbf8b0f89511fa11252
Diffstat (limited to 'client')
-rw-r--r--client/src/com/vaadin/client/Util.java19
1 files changed, 9 insertions, 10 deletions
diff --git a/client/src/com/vaadin/client/Util.java b/client/src/com/vaadin/client/Util.java
index 778f7c3861..99e6507d81 100644
--- a/client/src/com/vaadin/client/Util.java
+++ b/client/src/com/vaadin/client/Util.java
@@ -825,30 +825,29 @@ public class Util {
* @return true if the collections contain the same elements in the same
* order, false otherwise
*/
- public static boolean collectionsEquals(Collection collection1,
- Collection collection2) {
+ public static boolean collectionsEquals(Collection<?> collection1,
+ Collection<?> collection2) {
if (collection1 == null) {
return collection2 == null;
}
if (collection2 == null) {
return false;
}
- Iterator<Object> collection1Iterator = collection1.iterator();
- Iterator<Object> collection2Iterator = collection2.iterator();
+
+ if (collection1.size() != collection2.size()) {
+ return false;
+ }
+
+ Iterator<?> collection1Iterator = collection1.iterator();
+ Iterator<?> collection2Iterator = collection2.iterator();
while (collection1Iterator.hasNext()) {
- if (!collection2Iterator.hasNext()) {
- return false;
- }
Object collection1Object = collection1Iterator.next();
Object collection2Object = collection2Iterator.next();
if (collection1Object != collection2Object) {
return false;
}
}
- if (collection2Iterator.hasNext()) {
- return false;
- }
return true;
}