Skip to content

Commit efe4353

Browse files
committed
perf: Make adding and removing annotations somewhat more efficient
1 parent 689846c commit efe4353

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/annotationLayer.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,7 @@ var annotationLayer = function (arg) {
496496
* @fires geo.event.annotation.add
497497
*/
498498
this.addAnnotation = function (annotation, gcs, update) {
499-
var pos = $.inArray(annotation, m_annotations);
500-
if (pos < 0) {
499+
if (m_annotationIds[annotation.id()] === undefined) {
501500
while (m_this.annotationById(annotation.id())) {
502501
annotation.newId();
503502
}
@@ -530,12 +529,14 @@ var annotationLayer = function (arg) {
530529
* @param {geo.annotation} annotation The annotation to remove.
531530
* @param {boolean} [update] If `false`, don't update the layer after removing
532531
* the annotation.
532+
* @param {int} [pos] The posiiton of the annotation in the annotation list,
533+
* if known. This speeds up the process.
533534
* @returns {boolean} `true` if an annotation was removed.
534535
* @fires geo.event.annotation.remove
535536
*/
536-
this.removeAnnotation = function (annotation, update) {
537-
var pos = $.inArray(annotation, m_annotations);
538-
if (pos >= 0) {
537+
this.removeAnnotation = function (annotation, update, pos) {
538+
if (annotation.id && m_annotationIds[annotation.id()] !== undefined) {
539+
pos = $.inArray(annotation, m_annotations);
539540
if (annotation === m_this.currentAnnotation) {
540541
m_this.currentAnnotation = null;
541542
}
@@ -551,8 +552,9 @@ var annotationLayer = function (arg) {
551552
m_this.geoTrigger(geo_event.annotation.remove, {
552553
annotation: annotation
553554
});
555+
return true;
554556
}
555-
return pos >= 0;
557+
return false;
556558
};
557559

558560
/**
@@ -565,14 +567,13 @@ var annotationLayer = function (arg) {
565567
* @returns {number} The number of annotations that were removed.
566568
*/
567569
this.removeAllAnnotations = function (skipCreating, update) {
568-
var removed = 0, annotation, pos = 0;
569-
while (pos < m_annotations.length) {
570+
var removed = 0, annotation;
571+
for (let pos = m_annotations.length - 1; pos >= 0; pos -= 1) {
570572
annotation = m_annotations[pos];
571573
if (skipCreating && annotation.state() === geo_annotation.state.create) {
572-
pos += 1;
573574
continue;
574575
}
575-
m_this.removeAnnotation(annotation, false);
576+
m_this.removeAnnotation(annotation, false, pos);
576577
removed += 1;
577578
}
578579
if (removed && update !== false) {

0 commit comments

Comments
 (0)