Skip to content

Commit

Permalink
fix(performance): reduce object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmeinke committed Sep 14, 2017
1 parent 82011a2 commit 3cb37e0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
17 changes: 13 additions & 4 deletions src/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@
*/
const clone = value => {
if (Array.isArray(value)) {
return value.map(x => clone(x))
const arr = []

for (let i = 0, l = value.length; i < l; i++) {
arr.push(clone(value[ i ]))
}

return arr
} else if (value !== null && typeof value === 'object') {
return Object.keys(value).reduce((obj, key) => {
const obj = {}

for (let key in value) {
obj[ key ] = clone(value[ key ])
return obj
}, {})
}

return obj
}

return value
Expand Down
4 changes: 3 additions & 1 deletion src/color-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ const htmlColors = {
'yellowgreen': '#9ACD32'
}

const htmlColorKeys = Object.keys(htmlColors)

/**
* Converts a color string to a Color.
*
Expand Down Expand Up @@ -253,7 +255,7 @@ const rgb = str => str.startsWith('rgb(')
* @example
* html('limegreen')
*/
const html = str => Object.keys(htmlColors).indexOf(str) !== -1
const html = str => htmlColorKeys.indexOf(str) !== -1

/**
* Converts a hex string to a Color.
Expand Down
33 changes: 17 additions & 16 deletions src/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,7 @@ const frame = (timeline, at) => {

const shapePosition = (timeline.state.position - start) / (finish - start)

return frameShapeFromShape({
shape,
position: shapePosition,
middleware: timeline.middleware
})
return frameShapeFromShape(shape, shapePosition, timeline.middleware)
})
}

Expand Down Expand Up @@ -306,19 +302,18 @@ const frameShapeFromPlainShapeObject = ({ shapes: childPlainShapeObjects, ...pla
/**
* Creates a FrameShape from a Shape given the Position.
*
* @param {Object} opts
* @param {Middleware[]} opts.middleware
* @param {Position} opts.position
* @param {Shape} opts.shape
* @param {Shape} shape
* @param {Position} position
* @param {Middleware[]} middleware
*
* @returns {FrameShape}
*
* @example
* frameShapeFromShape({ position: 0.75, shape })
* frameShapeFromShape(shape, 0.75, [])
*/
const frameShapeFromShape = ({ middleware = [], position, shape }) => {
const fromIndex = shape.keyframes.reduce((currentFromIndex, { position: keyframePosition }, i) => (
position > keyframePosition ? i : currentFromIndex
const frameShapeFromShape = (shape, position, middleware) => {
const fromIndex = shape.keyframes.reduce((currentFromIndex, keyframe, i) => (
position > keyframe.position ? i : currentFromIndex
), 0)

const toIndex = fromIndex + 1
Expand Down Expand Up @@ -442,17 +437,23 @@ const tween = (from, to, easing, position) => {
throw new TypeError(errorMsg)
}

return from.map((f, i) => (tween(f, to[ i ], easing, position)))
const arr = []

for (let i = 0, l = from.length; i < l; i++) {
arr.push(tween(from[ i ], to[ i ], easing, position))
}

return arr
} else if (from !== null && typeof from === 'object') {
if (to !== null && typeof to !== 'object') {
throw new TypeError(errorMsg)
}

const obj = {}

Object.keys(from).map(k => {
for (let k in from) {
obj[ k ] = tween(from[ k ], to[ k ], easing, position)
})
}

return obj
} else if (typeof from === 'number') {
Expand Down
36 changes: 27 additions & 9 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ const apply = (value, func) => {
const v = func(value)

if (Array.isArray(v)) {
return v.map(x => apply(x, func))
const arr = []

for (let i = 0, l = v.length; i < l; i++) {
arr.push(apply(v[ i ], func))
}

return arr
}

if (v !== null && typeof v === 'object') {
const obj = {}

Object.keys(v).map(k => {
for (let k in v) {
obj[ k ] = apply(v[ k ], func)
})
}

return obj
}
Expand All @@ -50,9 +56,15 @@ const apply = (value, func) => {
* @example
* input({ foo: 1, bar: [ 2, 3 ] }, middleware)
*/
const input = (value, middleware) => middleware.reduce((v, m) => (
apply(v, m.input)
), value)
const input = (value, middleware) => {
let v = value

for (let i = 0, l = middleware.length; i < l; i++) {
v = apply(v, middleware[ i ].input)
}

return v
}

/**
* Runs each Middleware output function in reverse on a value.
Expand All @@ -65,8 +77,14 @@ const input = (value, middleware) => middleware.reduce((v, m) => (
* @example
* output({ foo: 1, bar: [ 2, 3 ] }, middleware)
*/
const output = (value, middleware) => [ ...middleware ].reverse().reduce((v, m) => (
apply(v, m.output)
), value)
const output = (value, middleware) => {
let v = value

for (let i = middleware.length - 1; i >= 0; i--) {
v = apply(v, middleware[ i ].output)
}

return v
}

export { input, output }

0 comments on commit 3cb37e0

Please sign in to comment.