Skip to content

Commit

Permalink
Speed up strongRound by avoiding string casting
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Nov 27, 2022
1 parent ae32acf commit 0f6de55
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ let arcThreshold;
* @type {number}
*/
let arcTolerance;
/**
* @type {number[]}
*/
const powCache = [1, 10, 100, 1000, 10000, 100000, 1000000];

/**
* @typedef {{
Expand Down Expand Up @@ -128,6 +132,13 @@ exports.fn = (root, params) => {
forceAbsolutePath,
};

// Expand powCache only if necessary
if (precision !== false && precision > powCache.length - 1) {
for (let i = powCache.length; i <= precision; i++) {
powCache.push(powCache[i - 1] * 10);
}
}

// invoke applyTransforms plugin
if (_applyTransforms) {
visit(
Expand Down Expand Up @@ -951,6 +962,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 = powCache[precision];
return Math.round(num * pow) / pow;
}

/**
* Decrease accuracy of floating-point numbers
* in path data keeping a specified number of decimals.
Expand All @@ -960,16 +981,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

0 comments on commit 0f6de55

Please sign in to comment.