From da841e92eb824e5397197fdda6d06d5203b55778 Mon Sep 17 00:00:00 2001 From: xiaoiver Date: Wed, 10 Aug 2022 16:05:39 +0800 Subject: [PATCH] fix: remove spread operator for better perf (#90) * fix: remove spread operator for better perf * chore: bump v3.2.3 Co-authored-by: yuqi.pyq --- package.json | 2 +- src/path/convert/path-2-absolute.ts | 6 +- src/path/parser/finalize-segment.ts | 4 +- src/path/process/arc-2-cubic.ts | 7 ++- src/path/process/clone-path.ts | 2 +- src/path/process/line-2-cubic.ts | 9 +-- src/path/process/normalize-segment.ts | 4 +- src/path/process/round-path.ts | 3 +- src/path/process/segment-2-cubic.ts | 18 +++--- src/path/util/equalize-segments.ts | 14 ++++- src/path/util/get-path-area.ts | 4 +- src/path/util/get-properties-at-length.ts | 2 +- src/path/util/path-length-factory.ts | 70 +++++++++++++++++------ src/path/util/segment-arc-factory.ts | 20 +++++-- src/path/util/segment-cubic-factory.ts | 20 +++++-- src/path/util/segment-quad-factory.ts | 20 +++++-- 16 files changed, 151 insertions(+), 54 deletions(-) diff --git a/package.json b/package.json index 8dc7039..2be4484 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@antv/util", - "version": "3.2.2", + "version": "3.2.3", "license": "MIT", "sideEffects": false, "main": "lib/index.js", diff --git a/src/path/convert/path-2-absolute.ts b/src/path/convert/path-2-absolute.ts index ebd80cb..4a02eec 100644 --- a/src/path/convert/path-2-absolute.ts +++ b/src/path/convert/path-2-absolute.ts @@ -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; diff --git a/src/path/parser/finalize-segment.ts b/src/path/parser/finalize-segment.ts index 594f677..ec1944d 100644 --- a/src/path/parser/finalize-segment.ts +++ b/src/path/parser/finalize-segment.ts @@ -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]) { diff --git a/src/path/process/arc-2-cubic.ts b/src/path/process/arc-2-cubic.ts index 3379d34..c6f6862 100644 --- a/src/path/process/arc-2-cubic.ts +++ b/src/path/process/arc-2-cubic.ts @@ -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; diff --git a/src/path/process/clone-path.ts b/src/path/process/clone-path.ts index a59957b..21762f2 100644 --- a/src/path/process/clone-path.ts +++ b/src/path/process/clone-path.ts @@ -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; } diff --git a/src/path/process/line-2-cubic.ts b/src/path/process/line-2-cubic.ts index 42736a7..3517408 100644 --- a/src/path/process/line-2-cubic.ts +++ b/src/path/process/line-2-cubic.ts @@ -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]; } diff --git a/src/path/process/normalize-segment.ts b/src/path/process/normalize-segment.ts index 22e4563..3c21633 100644 --- a/src/path/process/normalize-segment.ts +++ b/src/path/process/normalize-segment.ts @@ -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; diff --git a/src/path/process/round-path.ts b/src/path/process/round-path.ts index fda14ef..c56b89f 100644 --- a/src/path/process/round-path.ts +++ b/src/path/process/round-path.ts @@ -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; } diff --git a/src/path/process/segment-2-cubic.ts b/src/path/process/segment-2-cubic.ts index 50c33c0..77f6456 100644 --- a/src/path/process/segment-2-cubic.ts +++ b/src/path/process/segment-2-cubic.ts @@ -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'; @@ -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; diff --git a/src/path/util/equalize-segments.ts b/src/path/util/equalize-segments.ts index 00e0495..cf5b780 100644 --- a/src/path/util/equalize-segments.ts +++ b/src/path/util/equalize-segments.ts @@ -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) { diff --git a/src/path/util/get-path-area.ts b/src/path/util/get-path-area.ts index 372c35b..36bfdec 100644 --- a/src/path/util/get-path-area.ts +++ b/src/path/util/get-path-area.ts @@ -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; } diff --git a/src/path/util/get-properties-at-length.ts b/src/path/util/get-properties-at-length.ts index 7e520c4..330db3f 100644 --- a/src/path/util/get-properties-at-length.ts +++ b/src/path/util/get-properties-at-length.ts @@ -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; diff --git a/src/path/util/path-length-factory.ts b/src/path/util/path-length-factory.ts index 8c60cb4..3a40954 100644 --- a/src/path/util/path-length-factory.ts +++ b/src/path/util/path-length-factory.ts @@ -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 */ @@ -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]; @@ -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), + ), }, }; } diff --git a/src/path/util/segment-arc-factory.ts b/src/path/util/segment-arc-factory.ts index ef1ba48..ba14311 100644 --- a/src/path/util/segment-arc-factory.ts +++ b/src/path/util/segment-arc-factory.ts @@ -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), + ), }, }; } diff --git a/src/path/util/segment-cubic-factory.ts b/src/path/util/segment-cubic-factory.ts index c708a2a..cfd0d34 100644 --- a/src/path/util/segment-cubic-factory.ts +++ b/src/path/util/segment-cubic-factory.ts @@ -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), + ), }, }; } diff --git a/src/path/util/segment-quad-factory.ts b/src/path/util/segment-quad-factory.ts index b723284..9da66b0 100644 --- a/src/path/util/segment-quad-factory.ts +++ b/src/path/util/segment-quad-factory.ts @@ -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), + ), }, }; }