From 55d468c18491225a3cdfd7d5d220330a8150fc66 Mon Sep 17 00:00:00 2001 From: Colin Meinke Date: Thu, 14 Sep 2017 18:35:50 -0500 Subject: [PATCH] fix(performance): reduce object creation Refs #71 --- package.json | 4 ++-- src/render.js | 15 ++++++++------- src/timeline.js | 12 ++++++------ tests/timeline.js | 4 ++-- yarn.lock | 12 ++++++------ 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 70d9ab7..4c6fdf3 100644 --- a/package.json +++ b/package.json @@ -50,8 +50,8 @@ } }, "dependencies": { - "wilderness-core": "^2.2.0", - "wilderness-dom-node": "^1.3.0" + "wilderness-core": "^2.2.1", + "wilderness-dom-node": "^1.3.1" }, "description": "An SVG animation API", "devDependencies": { diff --git a/src/render.js b/src/render.js index 200ecae..f34cf54 100644 --- a/src/render.js +++ b/src/render.js @@ -98,10 +98,11 @@ const render = (container, ...shapesAndTimelines) => { * split([ shape, timeline ]) */ const split = shapesAndTimelines => { - const shapes = [] - const timelines = [] + const result = { shapes: [], timelines: [] } + + for (let i = 0, l = shapesAndTimelines.length; i < l; i++) { + const x = shapesAndTimelines[ i ] - shapesAndTimelines.map(x => { if (typeof x === 'object' && x.keyframes) { if (__DEV__) { if (x.timeline) { @@ -113,19 +114,19 @@ const split = shapesAndTimelines => { } } - shapes.push(x) + result.shapes.push(x) } else if (typeof x === 'object' && x.middleware && x.playbackOptions && x.state && x.timelineShapes) { if (__DEV__ && x.state.rendered) { throw new Error(`You cannot render the same timeline twice`) } - timelines.push(x) + result.timelines.push(x) } else if (__DEV__) { throw new Error(`The render function only takes shapes and timelines from the second argument onwards`) } - }) + } - return { shapes, timelines } + return result } export default render diff --git a/src/timeline.js b/src/timeline.js index af31903..cf4e94b 100644 --- a/src/timeline.js +++ b/src/timeline.js @@ -37,15 +37,14 @@ const play = (t, playbackOptions, at) => { * Calculate the active Timeline Shapes and update the corresponding Nodes. * Call recursively until there are no longer any active Timelines. * - * @param {Object} opts - * @param {number} [at] * @param {boolean} [bypassTickingCheck=false] * @param {boolean} [recurse=true] + * @param {number} [at] * * @example * tick() */ -const tick = ({ at, bypassTickingCheck = false, recurse = true } = {}) => { +const tick = (bypassTickingCheck = false, recurse = true, at) => { if (!ticking || bypassTickingCheck) { if (__DEV__ && typeof at !== 'undefined' && typeof at !== 'number') { throw new TypeError(`The tick functions at option must be of type number`) @@ -57,7 +56,8 @@ const tick = ({ at, bypassTickingCheck = false, recurse = true } = {}) => { const a = typeof at !== 'undefined' ? at : Date.now() const activeTimelines = timelines.filter(active) - activeTimelines.map(t => { + for (let i = 0, l = activeTimelines.length; i < l; i++) { + const t = activeTimelines[ i ] const frameShapes = frame(t, a) t.timelineShapes.map(({ shape }, i) => { @@ -65,10 +65,10 @@ const tick = ({ at, bypassTickingCheck = false, recurse = true } = {}) => { }) events(t) - }) + } if (activeTimelines.length && recurse) { - tick({ bypassTickingCheck: true }) + tick(true) } else { ticking = false } diff --git a/tests/timeline.js b/tests/timeline.js index a03878e..407ac1a 100644 --- a/tests/timeline.js +++ b/tests/timeline.js @@ -64,7 +64,7 @@ describe('play', () => { describe('tick', () => { it('should throw if passed an invalid at option', () => { - expect(() => tick({ at: 'potato', bypassTickingCheck: true })) + expect(() => tick(true, true, 'potato')) .to.throw('The tick functions at option must be of type number') }) @@ -86,7 +86,7 @@ describe('tick', () => { expect(s.node.toString()).to.eql(preTickExpectedEl.toString()) - tick({ at: 500, recurse: false }) + tick(false, false, 500) expect(s.node.toString()).to.eql(postTickExpectedEl.toString()) }) diff --git a/yarn.lock b/yarn.lock index 7af1a0a..e236f24 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4931,17 +4931,17 @@ wide-align@^1.1.0: dependencies: string-width "^1.0.2" -wilderness-core@^2.2.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/wilderness-core/-/wilderness-core-2.2.0.tgz#cf616e9052a234a3911ecfbec80e21bd0c4ab09a" +wilderness-core@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/wilderness-core/-/wilderness-core-2.2.1.tgz#7438109a34d1ef9c50213611641802bc63b98b6a" dependencies: points "^3.1.0" svg-points "^6.0.0" tween-functions "^1.2.0" -wilderness-dom-node@^1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/wilderness-dom-node/-/wilderness-dom-node-1.3.0.tgz#fcc082a6466c2c47e597f89120566a8d866f8f94" +wilderness-dom-node@^1.3.1: + version "1.3.1" + resolved "https://registry.yarnpkg.com/wilderness-dom-node/-/wilderness-dom-node-1.3.1.tgz#f2dd70d40fcffc34744c4e5ec92c35bc6472d2b7" dependencies: svg-points "^6.0.0"