Skip to content

Commit

Permalink
perf(lodash): remove _.omit and _.isEqual (#2111)
Browse files Browse the repository at this point in the history
* perf(lodash): remove use of omit

* perf(lodash): replace isEqual with shallowEqual

* fix(Dropdown|Search): restore SCU with shallowEqual
  • Loading branch information
levithomason authored Oct 2, 2017
1 parent b6fb22d commit 17584a9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/lib/objectDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export default (source, target) => _.transform(source, (res, val, key) => {
// deleted keys
if (!_.has(target, key)) res[key] = '[DELETED]'
// new keys / changed values
// Note, we tolerate isEqual here as this is a dev only utility and not included in production code
else if (!_.isEqual(val, target[key])) res[key] = target[key]
}, {})
13 changes: 7 additions & 6 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
makeDebugger,
META,
objectDiff,
shallowEqual,
useKeyOnly,
useKeyOrValueAndKey,
} from '../../lib'
Expand Down Expand Up @@ -395,10 +396,6 @@ export default class Dropdown extends Component {
if (open) this.open()
}

shouldComponentUpdate(nextProps, nextState) {
return !_.isEqual(nextProps, this.props) || !_.isEqual(nextState, this.state)
}

componentWillReceiveProps(nextProps) {
super.componentWillReceiveProps(nextProps)
debug('componentWillReceiveProps()')
Expand All @@ -424,17 +421,21 @@ export default class Dropdown extends Component {
}
/* eslint-enable no-console */

if (!_.isEqual(nextProps.value, this.props.value)) {
if (!shallowEqual(nextProps.value, this.props.value)) {
debug('value changed, setting', nextProps.value)
this.setValue(nextProps.value)
this.setSelectedIndex(nextProps.value)
}

if (!_.isEqual(nextProps.options, this.props.options)) {
if (!shallowEqual(nextProps.options, this.props.options)) {
this.setSelectedIndex(undefined, nextProps.options)
}
}

shouldComponentUpdate(nextProps, nextState) {
return !shallowEqual(nextProps, this.props) || !shallowEqual(nextState, this.state)
}

componentDidUpdate(prevProps, prevState) { // eslint-disable-line complexity
debug('componentDidUpdate()')
debug('to state:', objectDiff(prevState, this.state))
Expand Down
6 changes: 5 additions & 1 deletion src/modules/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ class Modal extends Component {
const unhandled = getUnhandledProps(Modal, this.props)
const portalPropNames = Portal.handledProps

const rest = _.omit(unhandled, portalPropNames)
const rest = _.reduce(unhandled, (acc, val, key) => {
if (!_.includes(portalPropNames, key)) acc[key] = val

return acc
}, {})
const portalProps = _.pick(unhandled, portalPropNames)

// wrap dimmer modals
Expand Down
6 changes: 5 additions & 1 deletion src/modules/Popup/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ export default class Popup extends Component {
const unhandled = getUnhandledProps(Popup, this.props)
const portalPropNames = Portal.handledProps

const rest = _.omit(unhandled, portalPropNames)
const rest = _.reduce(unhandled, (acc, val, key) => {
if (!_.includes(portalPropNames, key)) acc[key] = val

return acc
}, {})
const portalProps = _.pick(unhandled, portalPropNames)
const ElementType = getElementType(Popup, this.props)

Expand Down
11 changes: 6 additions & 5 deletions src/modules/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
META,
objectDiff,
partitionHTMLInputProps,
shallowEqual,
SUI,
useKeyOnly,
useValueAndKey,
Expand Down Expand Up @@ -210,21 +211,21 @@ export default class Search extends Component {
if (open) this.open()
}

shouldComponentUpdate(nextProps, nextState) {
return !_.isEqual(nextProps, this.props) || !_.isEqual(nextState, this.state)
}

componentWillReceiveProps(nextProps) {
super.componentWillReceiveProps(nextProps)
debug('componentWillReceiveProps()')
debug('changed props:', objectDiff(nextProps, this.props))

if (!_.isEqual(nextProps.value, this.props.value)) {
if (!shallowEqual(nextProps.value, this.props.value)) {
debug('value changed, setting', nextProps.value)
this.setValue(nextProps.value)
}
}

shouldComponentUpdate(nextProps, nextState) {
return !shallowEqual(nextProps, this.props) || !shallowEqual(nextState, this.state)
}

componentDidUpdate(prevProps, prevState) { // eslint-disable-line complexity
debug('componentDidUpdate()')
debug('to state:', objectDiff(prevState, this.state))
Expand Down

0 comments on commit 17584a9

Please sign in to comment.