Skip to content

Commit b3d8d83

Browse files
committed
perf: Speed up adding annotations
Track annotation ids on the annotation layer to speed up adding large numbers of annotations.
1 parent 0bf0f62 commit b3d8d83

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/annotationLayer.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ var annotationLayer = function (arg) {
9696
m_options,
9797
m_mode = null,
9898
m_annotations = [],
99+
m_annotationIds = {},
99100
m_features = [],
100101
m_labelFeature,
101102
m_labelLayer,
@@ -504,6 +505,7 @@ var annotationLayer = function (arg) {
504505
annotation: annotation
505506
});
506507
m_annotations.push(annotation);
508+
m_annotationIds[annotation.id()] = annotation;
507509
annotation.layer(m_this);
508510
var map = m_this.map();
509511
gcs = (gcs === null ? map.gcs() : (
@@ -537,6 +539,9 @@ var annotationLayer = function (arg) {
537539
if (annotation === m_this.currentAnnotation) {
538540
m_this.currentAnnotation = null;
539541
}
542+
if (m_annotationIds[annotation.id()] !== undefined) {
543+
delete m_annotationIds[annotation.id()];
544+
}
540545
annotation._exit();
541546
m_annotations.splice(pos, 1);
542547
if (update !== false) {
@@ -597,12 +602,7 @@ var annotationLayer = function (arg) {
597602
if (id !== undefined && id !== null) {
598603
id = +id; /* Cast to int */
599604
}
600-
var annotations = m_annotations.filter(function (annotation) {
601-
return annotation.id() === id;
602-
});
603-
if (annotations.length) {
604-
return annotations[0];
605-
}
605+
return m_annotationIds[id];
606606
};
607607

608608
/* A list of special modes */
@@ -1434,6 +1434,7 @@ var annotationLayer = function (arg) {
14341434
// Call super class exit
14351435
s_exit.call(m_this);
14361436
m_annotations = [];
1437+
m_annotationIds = {};
14371438
m_features = [];
14381439
return m_this;
14391440
};

0 commit comments

Comments
 (0)