https://www.loom.com/share/e70379b76e5141d984e773b70dd47921

1. The problem

Editing a simple geometry is janky on MBP M1.

janky-drag.mp4

The performance tab shows there is a janky piece and inside we have lots and lots of minor garbage collection events.

Screen Shot 2022-09-24 at 10.17.39 am.png

Let’s see what does the function do:

debug.mp4

Looking at the unminified code we see the function is called isEmptyDeep:

export function isEmptyDeep(l) {
  // thanks for the function, Felix Heck
  const flatten = (list) =>
    list
      .filter((x) => ![null, '', undefined].includes(x))
      .reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);

  return !flatten(l).length;
}

And it’s used inside Snapping.js/_createSnapList()

// also remove everything that has no coordinates yet
layers = layers.filter(
  (layer) =>
    layer._latlng || (layer._latlngs && !isEmptyDeep(layer._latlngs))
);

So for each layer on the map we’re gonna run isEmptyDeep, and we can either have a lot of layers or some very big layers. In my case it was a 25k points geometry. That directly has nothing to do with the little editing geometry, but for snapping, we were running isEmptyDeep on it.

Screen Shot 2022-09-24 at 12.56.26 pm.png

2. The solution

Take a moment and think what is wrong with isEmptyDeep?