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

path.digits #272

Merged
merged 8 commits into from
Dec 19, 2022
Merged

path.digits #272

merged 8 commits into from
Dec 19, 2022

Conversation

mbostock
Copy link
Member

Fixes #271. Related d3/d3-path#12.

@mbostock mbostock requested a review from Fil December 18, 2022 17:14
let i = 1;
this._ += strings[0];
for (const j = strings.length; i < j; ++i) {
this._ += +arguments[i].toFixed(digits) + strings[i];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need additional coercion here if the stream is passed non-numeric input.

@mbostock
Copy link
Member Author

I did some benchmarking in https://observablehq.com/d/07584fd387781785 and learned some things.

untitled (86)

  • Switching to string concatenation is faster than the current array.join method. 👍
  • Using template literals, and even a custom template literal tag, is fast. 👍
  • Coercing the result of toFixed to a number and then to a string to trim trailing zeroes is slow. 👎
  • A custom function for trimming trailing zeroes is faster. 👍
  • But much faster still is to use Math.round instead. 👍👍

So, I’d like to use the Math.round approach. We learned six years ago in d3/d3-path#10 (comment) that Math.round is faster than number.toFixed, so I’m just repeating that lesson here.

Copy link
Member

@Fil Fil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@mbostock
Copy link
Member Author

Related d3/d3-path#11 which also adopted string concatenation over array.join for faster performance.

@mbostock mbostock merged commit 18eb2fb into main Dec 19, 2022
@mbostock mbostock deleted the mbostock/path-digits branch December 19, 2022 21:56
marcelgerber added a commit to owid/owid-grapher that referenced this pull request Jan 26, 2023
mlbrgl pushed a commit to owid/owid-grapher that referenced this pull request Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Configurable precision for d3.geoPath
2 participants