From b1dcc98c0a603375df0c8187a02379566383d97c Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Sat, 1 Feb 2025 20:57:11 +0800 Subject: [PATCH] perf: reducing GC overhead --- package.json | 14 +++++++------- src/lib/engine.ts | 8 +++++--- test/penner.ts | 38 +------------------------------------- 3 files changed, 13 insertions(+), 47 deletions(-) diff --git a/package.json b/package.json index c475680..04029ed 100644 --- a/package.json +++ b/package.json @@ -13,20 +13,20 @@ }, "devDependencies": { "@rollup/plugin-terser": "^0.4.4", - "@rollup/plugin-typescript": "^12.1.0", + "@rollup/plugin-typescript": "^12.1.2", "@types/chai": "^4.3.16", "@types/jsdom": "^21.1.7", - "@types/mocha": "^10.0.8", + "@types/mocha": "^10.0.10", "@types/sinon": "^17.0.3", - "c8": "^10.1.2", + "c8": "^10.1.3", "chai": "^4.5.0", "jsdom": "^25.0.1", - "mocha": "^10.7.3", - "rollup": "^4.24.0", + "mocha": "^10.8.2", + "rollup": "^4.34.0", "sinon": "^19.0.2", "ts-node": "^10.9.2", - "tslib": "^2.7.0", - "typescript": "^5.6.2" + "tslib": "^2.8.1", + "typescript": "^5.7.3" }, "repository": { "type": "git", diff --git a/src/lib/engine.ts b/src/lib/engine.ts index e96b8e3..a472d1e 100644 --- a/src/lib/engine.ts +++ b/src/lib/engine.ts @@ -2,6 +2,8 @@ import type Anime from "../Anime"; import type { KeyFrameProp } from "../types"; import penner from "./penner"; +const pennerFn = penner(); + // 目前仅支持translate const validTransform = ["translateX", "translateY", "translateZ"]; @@ -138,7 +140,7 @@ export default (anime: Anime) => { } = (dest as KeyFrameProp)[i - 1]; origin = i <= 1 ? origin : (dest as KeyFrameProp)[i - 2].value; if (current <= start + duration + startTimeStamp) { - elapsed = penner()[easing]()( + elapsed = pennerFn[easing]()( (current - start - startTimeStamp) / duration ); @@ -151,7 +153,7 @@ export default (anime: Anime) => { // 支持 {value: 1, duration: 500, easing: 'linear'} const { value, duration, easing = anime.easing } = dest; if (current <= start + duration) { - elapsed = penner()[easing]()((current - start) / duration); + elapsed = pennerFn[easing]()((current - start) / duration); change(target, origin, elapsed, value, key); } else if (final) { change(target, origin, elapsed, value, key, final); @@ -189,7 +191,7 @@ export default (anime: Anime) => { anime.isPlay = false; } else { if (current >= start) { - const elapsed = penner()[anime.easing]()( + const elapsed = pennerFn[anime.easing]()( (current - start) / anime.duration ); if (isValid) changeAll(elapsed, current); diff --git a/test/penner.ts b/test/penner.ts index 3b3982a..dbbb263 100644 --- a/test/penner.ts +++ b/test/penner.ts @@ -3,196 +3,160 @@ const should = chai.should(); import penner from "../src/lib/penner"; describe("penner", () => { + const p = penner(); it("linear", () => { - const p = penner(); p.linear()(0.5).should.equal(0.5); }); it("easeInSine", () => { - const p = penner(); p.easeInSine()(0.5).should.equal(0.2928932188134524); }); it("easeOutSine", () => { - const p = penner(); p.easeOutSine()(0.5).should.equal(0.7071067811865476); }); it("easeInOutSine", () => { - const p = penner(); p.easeInOutSine()(0.5).should.equal(0.5); }); it("easeOutInSine", () => { - const p = penner(); p.easeOutInSine()(0.5).should.equal(0.5); }); it("easeInQuad", () => { - const p = penner(); p.easeInQuad()(0.5).should.equal(0.25); }); it("easeOutQuad", () => { - const p = penner(); p.easeOutQuad()(0.5).should.equal(0.75); }); it("easeInOutQuad", () => { - const p = penner(); p.easeInOutQuad()(0.5).should.equal(0.5); }); it("easeOutInQuad", () => { - const p = penner(); p.easeOutInQuad()(0.5).should.equal(0.5); }); it("easeInCubic", () => { - const p = penner(); p.easeInCubic()(0.5).should.equal(0.125); }); it("easeOutCubic", () => { - const p = penner(); p.easeOutCubic()(0.5).should.equal(0.875); }); it("easeInOutCubic", () => { - const p = penner(); p.easeInOutCubic()(0.5).should.equal(0.5); }); it("easeOutInCubic", () => { - const p = penner(); p.easeOutInCubic()(0.5).should.equal(0.5); }); it("easeInQuart", () => { - const p = penner(); p.easeInQuart()(0.5).should.equal(0.0625); }); it("easeOutQuart", () => { - const p = penner(); p.easeOutQuart()(0.5).should.equal(0.9375); }); it("easeInOutQuart", () => { - const p = penner(); p.easeInOutQuart()(0.5).should.equal(0.5); }); it("easeOutInQuart", () => { - const p = penner(); p.easeOutInQuart()(0.5).should.equal(0.5); }); it("easeInQuint", () => { - const p = penner(); p.easeInQuint()(0.5).should.equal(0.03125); }); it("easeOutQuint", () => { - const p = penner(); p.easeOutQuint()(0.5).should.equal(0.96875); }); it("easeInOutQuint", () => { - const p = penner(); p.easeInOutQuint()(0.5).should.equal(0.5); }); it("easeOutInQuint", () => { - const p = penner(); p.easeOutInQuint()(0.5).should.equal(0.5); }); it("easeInExpo", () => { - const p = penner(); p.easeInExpo()(0.5).should.equal(0.03125); }); it("easeOutExpo", () => { - const p = penner(); p.easeOutExpo()(0.5).should.equal(0.96875); }); it("easeInOutExpo", () => { - const p = penner(); p.easeInOutExpo()(0.5).should.equal(0.5); }); it("easeOutInExpo", () => { - const p = penner(); p.easeOutInExpo()(0.5).should.equal(0.5); }); it("easeInCirc", () => { - const p = penner(); p.easeInCirc()(0.5).should.equal(0.1339745962155614); }); it("easeOutCirc", () => { - const p = penner(); p.easeOutCirc()(0.5).should.equal(0.8660254037844386); }); it("easeInOutCirc", () => { - const p = penner(); p.easeInOutCirc()(0.5).should.equal(0.5); }); it("easeOutInCirc", () => { - const p = penner(); p.easeOutInCirc()(0.5).should.equal(0.5); }); it.skip("easeInBack", () => { // not real easeInBack - const p = penner(); p.easeInBack()(0.5).should.equal(-0.0876975); }); it.skip("easeOutBack", () => { // not real easeOutBack - const p = penner(); p.easeOutBack()(0.5).should.equal(1.0876975); }); it("easeInOutBack", () => { // not real easeInOutBack - const p = penner(); p.easeInOutBack()(0.5).should.equal(0.5); }); it("easeOutInBack", () => { // not real easeOutInBack - const p = penner(); p.easeOutInBack()(0.5).should.equal(0.5); }); it.skip("easeInBounce", () => { // not real easeInBounce - const p = penner(); p.easeInBounce()(0.5).should.equal(0.046875); }); it.skip("easeOutBounce", () => { // not real easeOutBounce - const p = penner(); p.easeOutBounce()(0.5).should.equal(0.953125); }); it("easeInOutBounce", () => { // not real easeInOutBounce - const p = penner(); p.easeInOutBounce()(0.5).should.equal(0.5); }); it("easeOutInBounce", () => { // not real easeOutInBounce - const p = penner(); p.easeOutInBounce()(0.5).should.equal(0.5); }); });