inocan-group / vue3-google-map

A set of composable components for easy use of Google Maps in your Vue 3 projects.
https://vue3-google-map.com
MIT License
264 stars 51 forks source link

CustomMarker draw method forces layout reflow #162

Closed halvtomat closed 9 months ago

halvtomat commented 9 months ago

I am using this library in a project with a few hundred customMarkers and noticed that the app is very laggy when zooming in and out on the map. When analyzing with devtools I noticed that on each zoom event the browser was forced to recalculate the layout and styling for each of my custom markers which is slow.

The reflow originates from the draw method in src/utils/index.ts.

// src/utils/index.ts

draw() {
      if (!this.element) return;

      const overlayProjection = this.getProjection();
      const point = overlayProjection?.fromLatLngToDivPixel(this.getPosition());

      if (point) {
        this.element.style.position = "absolute";
        const height = this.element.offsetHeight; // <-- THIS LINE
        const width = this.element.offsetWidth; // <-- AND THIS LINE
        let x: number, y: number;
        switch (this.opts.anchorPoint) {

If you check this link you can see that accessing element.offsetHeight and element.offsetWidth forces a reflow.

This issue can be solved by including a size option for the custom markers which can be used instead of the element attributes.

        const height = this.opts.size && this.opts.size.height ? this.opts.size.height : this.element.offsetHeight;
        const width = this.opts.size && this.opts.size.width ? this.opts.size.width : this.element.offsetWidth;

I have solved it this way in a fork I made, it improved map performance in my app by 90%. Submitting a PR soon.

halvtomat commented 9 months ago

Addressed in #163