Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(map): optimize geoPathsFor method #1792

Merged
merged 3 commits into from
Jan 26, 2023
Merged

Conversation

marcelgerber
Copy link
Member

Okay, well, I must admit I got sidetracked when working on map annotations ;)

I noticed the way we first generate a SVG path for a country, then tear it apart to round numbers to one decimal place, and then reconstructing the SVG path is not very performant.

It turns out that d3 allows passing a context to the projection functions. The context is normally used to render to, say, a canvas element, but it's also perfectly fine for our use case here.

And indeed, in my very unscientific testing, geoPathsFor takes:

  • 30-40ms before this optimization
  • 12-15ms now

The return value is totally unchanged, which the SVG tester will confirm.

@marcelgerber
Copy link
Member Author

With the latest commit, there are now expected non-visual SVG diffs:
We now use Math.round(x * 10) / 10 rather than the previous x.toFixed(1). This makes it so a L538.0,288.9 becomes L538,288.9, for example (i.e. we drop the unnecessary trailing zero).

Also, once we get the chance to update to d3-geo@3.1.0, we can use its now-inbuilt feature instead (d3/d3-geo#272).

@marcelgerber marcelgerber merged commit 57b8e6a into master Jan 26, 2023
@marcelgerber marcelgerber deleted the map-geo-paths-perf branch January 26, 2023 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant