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

Speed up strongRound by avoiding string casting #1716

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,16 @@ function getIntersection(coords) {
}
}

/**
* Does the same as `Number.prototype.toFixed` but without casting
* the return value to a string.
* @type {(num: number, precision: number) => number}
*/
function toFixed(num, precision) {
const pow = 10 ** precision;
return Math.round(num * pow) / pow;
Comment on lines +960 to +961

Choose a reason for hiding this comment

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

Hi @marvinhagemeister, I was reading your blog post and I noticed this potential issue:

precision can go up to 19 if I read this file right (but seems to default to 3?) so you can lose some... ah, er... precision when multiplying it with another large number:

Math.round(Number.MAX_SAFE_INTEGER * 10**19) / 10**19  // off by one
Math.round(4294967295 * 10**19) / 10**19  // 4294967295.0000005
Math.round(16777215 * 10**19) / 10**19  // 16777214.999999998

I assume that's okay here because num won't be that big but I can't say for sure from context.

Copy link
Contributor Author

@marvinhagemeister marvinhagemeister Jan 10, 2023

Choose a reason for hiding this comment

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

Good catch, yeah with higher precision values this can become an issue. The precision setting is typically used to reduce precision to make the SVG smaller. So far I've only seen precision settings of 1-3 in the wild.

}

/**
* Decrease accuracy of floating-point numbers
* in path data keeping a specified number of decimals.
Expand All @@ -960,16 +970,14 @@ function getIntersection(coords) {
* @type {(data: number[]) => number[]}
*/
function strongRound(data) {
for (var i = data.length; i-- > 0; ) {
// @ts-ignore
if (data[i].toFixed(precision) != data[i]) {
// @ts-ignore
var rounded = +data[i].toFixed(precision - 1);
const precisionNum = precision || 0;
for (let i = data.length; i-- > 0; ) {
const fixed = toFixed(data[i], precisionNum);
if (fixed !== data[i]) {
const rounded = toFixed(data[i], precisionNum - 1);
data[i] =
// @ts-ignore
+Math.abs(rounded - data[i]).toFixed(precision + 1) >= error
? // @ts-ignore
+data[i].toFixed(precision)
toFixed(Math.abs(rounded - data[i]), precisionNum + 1) >= error
? fixed
: rounded;
}
}
Expand Down