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

fix: remove spread operator for better perf #90

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/util",
"version": "3.2.2",
"version": "3.2.3",
"license": "MIT",
"sideEffects": false,
"main": "lib/index.js",
Expand Down
6 changes: 4 additions & 2 deletions src/path/convert/path-2-absolute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ export function path2Absolute(pathInput: string | PathArray): AbsoluteArray {
// https://stackoverflow.com/a/50753272/803358
const absValues = values.map((n, j) => n + (j % 2 ? y : x));
// for n, l, c, s, q, t
absoluteSegment = [absCommand, ...absValues] as AbsoluteSegment;
// @ts-ignore
absoluteSegment = [absCommand].concat(absValues) as AbsoluteSegment;
}
}
} else {
absoluteSegment = [absCommand, ...values] as AbsoluteSegment;
// @ts-ignore
absoluteSegment = [absCommand].concat(values) as AbsoluteSegment;
}

const segLength = absoluteSegment.length;
Expand Down
4 changes: 2 additions & 2 deletions src/path/parser/finalize-segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export function finalizeSegment(path: PathParser) {
// https://github.com/rveciana/svg-path-properties/blob/master/src/parse.ts
if (LK === 'm' && data.length > 2) {
// @ts-ignore
path.segments.push([pathCommand, ...data.splice(0, 2)]);
path.segments.push([pathCommand].concat(data.splice(0, 2)));
LK = 'l';
pathCommand = pathCommand === 'm' ? 'l' : 'L';
} else {
// @ts-ignore
path.segments.push([pathCommand, ...data.splice(0, paramsCount[LK])]);
path.segments.push([pathCommand].concat(data.splice(0, paramsCount[LK])));
}

if (!paramsCount[LK]) {
Expand Down
7 changes: 5 additions & 2 deletions src/path/process/arc-2-cubic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ export function arcToCubic(
m2[0] = 2 * m1[0] - m2[0];
m2[1] = 2 * m1[1] - m2[1];
if (recursive) {
return [...m2, ...m3, ...m4, ...res];
return m2.concat(m3, m4, res);
// return [...m2, ...m3, ...m4, ...res];
}
res = [...m2, ...m3, ...m4, ...res];

res = m2.concat(m3, m4, res);
// res = [...m2, ...m3, ...m4, ...res];
const newres = [];
for (let i = 0, ii = res.length; i < ii; i += 1) {
newres[i] = i % 2 ? rotateVector(res[i - 1], res[i], rad).y : rotateVector(res[i], res[i + 1], rad).x;
Expand Down
2 changes: 1 addition & 1 deletion src/path/process/clone-path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PathArray, PathSegment } from '../types';

export function clonePath(path: PathArray | PathSegment): PathArray {
return path.map((x) => (Array.isArray(x) ? [...x] : x)) as PathArray;
return path.map((x) => (Array.isArray(x) ? [].concat(x) : x)) as PathArray;
}
9 changes: 5 additions & 4 deletions src/path/process/line-2-cubic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ export function lineToCubic(x1: number, y1: number, x2: number, y2: number) {
const p4 = midPoint(p2, p3, t);
const p5 = midPoint(p3, p4, t);
const p6 = midPoint(p4, p5, t);
const seg1 = [...p0, ...p2, ...p4, ...p6, t];

// const seg1 = [...p0, ...p2, ...p4, ...p6, t];
// @ts-ignore
const cp1 = segmentLineFactory(...seg1).point;
const seg2 = [...p6, ...p5, ...p3, ...p1, 0];
const cp1 = segmentLineFactory(p0[0], p0[1], p2[0], p2[1], p4[0]).point;
// const seg2 = [...p6, ...p5, ...p3, ...p1, 0];
// @ts-ignore
const cp2 = segmentLineFactory(...seg2).point;
const cp2 = segmentLineFactory(p6[0], p6[1], p5[0], p5[1], p3[0]).point;

return [cp1.x, cp1.y, cp2.x, cp2.y, x2, y2];
}
4 changes: 2 additions & 2 deletions src/path/process/normalize-segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export function normalizeSegment(segment: PathSegment, params: any): NormalSegme
const y1 = py1 * 2 - py2;
params.x1 = x1;
params.y1 = y1;
result = ['C', x1, y1, ...values] as CSegment;
result = ['C', x1, y1].concat(values) as CSegment;
} else if (pathCommand === 'T') {
const qx = px1 * 2 - params.qx;
const qy = py1 * 2 - params.qy;
params.qx = qx;
params.qy = qy;
result = ['Q', qx, qy, ...values] as QSegment;
result = ['Q', qx, qy].concat(values) as QSegment;
} else if (pathCommand === 'Q') {
const [nqx, nqy] = values;
params.qx = nqx;
Expand Down
3 changes: 2 additions & 1 deletion src/path/process/round-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function roundPath(path: PathArray, round: number | 'off'): PathArray {
.slice(1)
.map(Number)
.map((n) => (round ? Math.round(n * pow) / pow : Math.round(n)));
return [pi[0], ...values];
// @ts-ignore
return [pi[0]].concat(values);
}) as PathArray;
}
18 changes: 11 additions & 7 deletions src/path/process/segment-2-cubic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PathSegment, ParserParams, CSegment, MSegment } from '../types';
import type { PathSegment, ParserParams, CSegment, MSegment, CubicSegment } from '../types';
import { arcToCubic } from './arc-2-cubic';
import { quadToCubic } from './quad-2-cubic';
import { lineToCubic } from './line-2-cubic';
Expand All @@ -21,24 +21,28 @@ export function segmentToCubic(segment: PathSegment, params: ParserParams): CSeg
params.y = y;
return segment;
case 'A':
args = [px1, py1, ...values];
args = [px1, py1].concat(values);
// @ts-ignore
return ['C', ...arcToCubic(...args)] as CubicSegment;
return ['C'].concat(
arcToCubic(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]),
) as CubicSegment;
case 'Q':
params.qx = x;
params.qy = y;
args = [px1, py1, ...values];
args = [px1, py1].concat(values);
// @ts-ignore
return ['C', ...quadToCubic(...args)] as CubicSegment;
return ['C'].concat(quadToCubic(args[0], args[1], args[2], args[3], args[4], args[5])) as CubicSegment;
case 'L':
return ['C', ...lineToCubic(px1, py1, x, y)] as CSegment;
// @ts-ignore
return ['C'].concat(lineToCubic(px1, py1, x, y)) as CSegment;
case 'Z':
// prevent NaN from divide 0
if (px1 === px && py1 === py) {
return ['C', px1, py1, px, py, px, py];
}

return ['C', ...lineToCubic(px1, py1, px, py)] as CSegment;
// @ts-ignore
return ['C'].concat(lineToCubic(px1, py1, px, py)) as CSegment;
default:
}
return segment as CSegment;
Expand Down
14 changes: 13 additions & 1 deletion src/path/util/equalize-segments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ function getCurveArray(segments: PathArray) {
const segmentData = i && pathArray[i - 1].slice(-2).concat(segment.slice(1));

// @ts-ignore
const curveLength = i ? segmentCubicFactory(...segmentData).length : 0;
const curveLength = i
? segmentCubicFactory(
segmentData[0],
segmentData[1],
segmentData[2],
segmentData[3],
segmentData[4],
segmentData[5],
segmentData[6],
segmentData[7],
segmentData[8],
).length
: 0;

let subsegs;
if (i) {
Expand Down
4 changes: 3 additions & 1 deletion src/path/util/get-path-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export function getPathArea(path: PathArray) {
return 0;
default:
// @ts-ignore
len = getCubicSegArea(x, y, ...seg.slice(1));
const [c1x, c1y, c2x, c2y, x2, y2] = seg.slice(1);

len = getCubicSegArea(x, y, c1x, c1y, c2x, c2y, x2, y2);
[x, y] = seg.slice(-2);
return len;
}
Expand Down
2 changes: 1 addition & 1 deletion src/path/util/get-properties-at-length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function getPropertiesAtLength(pathInput: string | PathArray, distance?:
throw TypeError(pathArray);
}

let pathTemp: PathArray = [...pathArray];
let pathTemp: PathArray = pathArray.slice() as PathArray;
let pathLength = getTotalLength(pathTemp);
let index = pathTemp.length - 1;
let lengthAtSegment = 0;
Expand Down
70 changes: 53 additions & 17 deletions src/path/util/path-length-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
seg = path[i];
[pathCommand] = seg;
isM = pathCommand === 'M';
data = !isM ? [x, y, ...seg.slice(1)] : data;
data = !isM ? [x, y].concat(seg.slice(1)) : data;

// this segment is always ZERO
/* istanbul ignore else */
Expand All @@ -49,29 +49,53 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
POINT = min;
}
} else if (pathCommand === 'L') {
// @ts-ignore
({ length, min, max, point } = segmentLineFactory(...data, (distance || 0) - LENGTH));
({ length, min, max, point } = segmentLineFactory(data[0], data[1], data[2], data[3], (distance || 0) - LENGTH));
} else if (pathCommand === 'A') {
// @ts-ignore
({ length, min, max, point } = segmentArcFactory(...data, (distance || 0) - LENGTH));
({ length, min, max, point } = segmentArcFactory(
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
data[8],
(distance || 0) - LENGTH,
));
} else if (pathCommand === 'C') {
// @ts-ignore
({ length, min, max, point } = segmentCubicFactory(...data, (distance || 0) - LENGTH));
({ length, min, max, point } = segmentCubicFactory(
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
(distance || 0) - LENGTH,
));
} else if (pathCommand === 'Q') {
// @ts-ignore
({ length, min, max, point } = segmentQuadFactory(...data, (distance || 0) - LENGTH));
({ length, min, max, point } = segmentQuadFactory(
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
(distance || 0) - LENGTH,
));
} else if (pathCommand === 'Z') {
data = [x, y, mx, my];
// @ts-ignore
({ length, min, max, point } = segmentLineFactory(...data, (distance || 0) - LENGTH));
({ length, min, max, point } = segmentLineFactory(data[0], data[1], data[2], data[3], (distance || 0) - LENGTH));
}

if (distanceIsNumber && LENGTH < distance && LENGTH + length >= distance) {
POINT = point;
}

MAX = [...MAX, max];
MIN = [...MIN, min];
MAX.push(max);
MIN.push(min);
LENGTH += length;

[x, y] = pathCommand !== 'Z' ? seg.slice(-2) : [mx, my];
Expand All @@ -87,12 +111,24 @@ export function pathLengthFactory(pathInput: string | PathArray, distance?: numb
length: LENGTH,
point: POINT,
min: {
x: Math.min(...MIN.map((n) => n.x)),
y: Math.min(...MIN.map((n) => n.y)),
x: Math.min.apply(
null,
MIN.map((n) => n.x),
),
y: Math.min.apply(
null,
MIN.map((n) => n.y),
),
},
max: {
x: Math.max(...MAX.map((n) => n.x)),
y: Math.max(...MAX.map((n) => n.y)),
x: Math.max.apply(
null,
MAX.map((n) => n.x),
),
y: Math.max.apply(
null,
MAX.map((n) => n.y),
),
},
};
}
20 changes: 16 additions & 4 deletions src/path/util/segment-arc-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,24 @@ export function segmentArcFactory(
length: LENGTH,
point: POINT,
min: {
x: Math.min(...POINTS.map((n) => n.x)),
y: Math.min(...POINTS.map((n) => n.y)),
x: Math.min.apply(
null,
POINTS.map((n) => n.x),
),
y: Math.min.apply(
null,
POINTS.map((n) => n.y),
),
},
max: {
x: Math.max(...POINTS.map((n) => n.x)),
y: Math.max(...POINTS.map((n) => n.y)),
x: Math.max.apply(
null,
POINTS.map((n) => n.x),
),
y: Math.max.apply(
null,
POINTS.map((n) => n.y),
),
},
};
}
20 changes: 16 additions & 4 deletions src/path/util/segment-cubic-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,24 @@ export function segmentCubicFactory(
length: LENGTH,
point: POINT,
min: {
x: Math.min(...POINTS.map((n) => n.x)),
y: Math.min(...POINTS.map((n) => n.y)),
x: Math.min.apply(
null,
POINTS.map((n) => n.x),
),
y: Math.min.apply(
null,
POINTS.map((n) => n.y),
),
},
max: {
x: Math.max(...POINTS.map((n) => n.x)),
y: Math.max(...POINTS.map((n) => n.y)),
x: Math.max.apply(
null,
POINTS.map((n) => n.x),
),
y: Math.max.apply(
null,
POINTS.map((n) => n.y),
),
},
};
}
20 changes: 16 additions & 4 deletions src/path/util/segment-quad-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,24 @@ export function segmentQuadFactory(
length: LENGTH,
point: POINT,
min: {
x: Math.min(...POINTS.map((n) => n.x)),
y: Math.min(...POINTS.map((n) => n.y)),
x: Math.min.apply(
null,
POINTS.map((n) => n.x),
),
y: Math.min.apply(
null,
POINTS.map((n) => n.y),
),
},
max: {
x: Math.max(...POINTS.map((n) => n.x)),
y: Math.max(...POINTS.map((n) => n.y)),
x: Math.max.apply(
null,
POINTS.map((n) => n.x),
),
y: Math.max.apply(
null,
POINTS.map((n) => n.y),
),
},
};
}