Skip to content

Commit

Permalink
fix(performance): reduce object creation
Browse files Browse the repository at this point in the history
Refs #71
  • Loading branch information
colinmeinke committed Sep 14, 2017
1 parent 56aeaf4 commit 55d468c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 8 additions & 7 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
12 changes: 6 additions & 6 deletions src/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -57,18 +56,19 @@ 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) => {
updateNode(shape.node, frameShapes[ i ])
})

events(t)
})
}

if (activeTimelines.length && recurse) {
tick({ bypassTickingCheck: true })
tick(true)
} else {
ticking = false
}
Expand Down
4 changes: 2 additions & 2 deletions tests/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand All @@ -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())
})
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 55d468c

Please sign in to comment.