Skip to content

Commit 236afb2

Browse files
authored
perf: Simplified debounce, get rid of lodash dependency 🗜 (#106)
* perf: Simplified debounce, get rid of lodash dependency * perf: Debounce with leading edge * refactor: Move debounce to utils * perf: Remove proptypes from production build * refactor: Do not remove proptypes
1 parent 3220c67 commit 236afb2

File tree

8 files changed

+61
-23
lines changed

8 files changed

+61
-23
lines changed

.babelrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
"test": {
66
"presets": ["es2015", "stage-0", "react"],
77
"plugins": ["istanbul"]
8-
},
9-
"production": {
10-
"plugins": ["transform-react-remove-prop-types"]
118
}
129
}
1310
}

docs/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"author": "pandah <hrusikesh.panda@dowjones.com>",
2424
"license": "MIT",
2525
"scripts": {
26-
"build": "rimraf dist/**/* && webpack --config webpack.config.js --bail --mode=production",
27-
"build:docs": "yarn build && webpack --config docs/webpack.config.js --bail --mode=production",
26+
"build": "rimraf dist/**/* && cross-env NODE_ENV=production webpack --config webpack.config.js --bail --mode=production",
27+
"build:docs": "yarn build && cross-env NODE_ENV=production webpack --config docs/webpack.config.js --bail --mode=production",
2828
"commit": "git-cz",
2929
"commitmsg": "commitlint -e $GIT_PARAMS",
3030
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
@@ -42,7 +42,6 @@
4242
"dependencies": {
4343
"array.partial": "^1.0.4",
4444
"classnames": "^2.2.5",
45-
"lodash.debounce": "^4.0.8",
4645
"react-infinite-scroll-component": "^4.0.2"
4746
},
4847
"devDependencies": {
@@ -102,20 +101,31 @@
102101
"webpack": "^4.6.0",
103102
"webpack-bundle-analyzer": "^2.9.2",
104103
"webpack-cli": "^2.0.9",
105-
"why-did-you-update": "^0.1.1",
106-
"webpack-serve": "^0.3.1"
104+
"webpack-serve": "^0.3.1",
105+
"why-did-you-update": "^0.1.1"
107106
},
108107
"peerDependencies": {
109108
"react": "^15.0.0 || ^16.0.0"
110109
},
111110
"ava": {
112-
"files": ["src/**/*.test.js"],
113-
"require": ["babel-register", "ignore-styles", "jsdom-global/register", "./setupEnzyme"],
111+
"files": [
112+
"src/**/*.test.js"
113+
],
114+
"require": [
115+
"babel-register",
116+
"ignore-styles",
117+
"jsdom-global/register",
118+
"./setupEnzyme"
119+
],
114120
"babel": {
115121
"testOptions": {
116122
"babelrc": false,
117-
"plugins": ["@babel/plugin-syntax-jsx"],
118-
"presets": ["@babel/preset-stage-3"]
123+
"plugins": [
124+
"@babel/plugin-syntax-jsx"
125+
],
126+
"presets": [
127+
"@babel/preset-stage-3"
128+
]
119129
}
120130
},
121131
"snapshotDir": "__snapshots__"

src/input/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React, { PureComponent } from 'react'
22
import PropTypes from 'prop-types'
33
import cn from 'classnames/bind'
4-
import debounce from 'lodash.debounce'
54
import Tag from '../tag'
65
import styles from './index.css'
7-
import { getDataset } from '../utils'
6+
import { getDataset, debounce } from '../utils'
87

98
const cx = cn.bind(styles)
109

@@ -31,13 +30,7 @@ class Input extends PureComponent {
3130

3231
constructor(props) {
3332
super(props)
34-
this.delayedCallback = debounce(
35-
e => {
36-
this.props.onInputChange(e.target.value)
37-
},
38-
50,
39-
{ leading: true }
40-
)
33+
this.delayedCallback = debounce(e => this.props.onInputChange(e.target.value), 300)
4134
}
4235

4336
handleInputChange = e => {

src/utils/debounce.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Modified debounce that always invokes on leading edge
3+
* See unmodified: https://gist.github.com/mrchief/a7e8938ee96774f05644905b37f09536
4+
*/
5+
export default (func, wait) => {
6+
let timeout
7+
8+
return (...args) => {
9+
const later = () => {
10+
timeout = null
11+
func(...args)
12+
}
13+
14+
// timeout will be undefined the first time (leading edge)
15+
// so the callback will get executed once on leading edge
16+
const callNow = !timeout
17+
18+
clearTimeout(timeout)
19+
timeout = setTimeout(later, wait)
20+
21+
if (callNow) func(...args)
22+
}
23+
}

src/utils/debounce.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import test from 'ava'
2+
3+
import debounce from './debounce'
4+
5+
test.cb("calls on leading edge and doesn't call until wait ends", t => {
6+
t.plan(2)
7+
const debounced = debounce(() => t.pass(), 50)
8+
debounced()
9+
debounced()
10+
debounced()
11+
setTimeout(() => {
12+
t.end()
13+
}, 70)
14+
})

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { default as getDataset } from './dataset'
22
export { default as mapToObject } from './mapToObject'
33
export { default as isEmpty } from './isEmpty'
44
export { default as isOutsideClick } from './isOutsideClick'
5+
export { default as debounce } from './debounce'

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6362,7 +6362,7 @@ lodash.clonedeepwith@^4.5.0:
63626362
version "4.5.0"
63636363
resolved "https://registry.yarnpkg.com/lodash.clonedeepwith/-/lodash.clonedeepwith-4.5.0.tgz#6ee30573a03a1a60d670a62ef33c10cf1afdbdd4"
63646364

6365-
lodash.debounce@^4.0.3, lodash.debounce@^4.0.8:
6365+
lodash.debounce@^4.0.3:
63666366
version "4.0.8"
63676367
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
63686368

0 commit comments

Comments
 (0)