-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
8 changed files
with
61 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,8 @@ | |
"author": "pandah <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "rimraf dist/**/* && webpack --config webpack.config.js --bail --mode=production", | ||
"build:docs": "yarn build && webpack --config docs/webpack.config.js --bail --mode=production", | ||
"build": "rimraf dist/**/* && cross-env NODE_ENV=production webpack --config webpack.config.js --bail --mode=production", | ||
"build:docs": "yarn build && cross-env NODE_ENV=production webpack --config docs/webpack.config.js --bail --mode=production", | ||
"commit": "git-cz", | ||
"commitmsg": "commitlint -e $GIT_PARAMS", | ||
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls", | ||
|
@@ -42,7 +42,6 @@ | |
"dependencies": { | ||
"array.partial": "^1.0.4", | ||
"classnames": "^2.2.5", | ||
"lodash.debounce": "^4.0.8", | ||
"react-infinite-scroll-component": "^4.0.2" | ||
}, | ||
"devDependencies": { | ||
|
@@ -102,20 +101,31 @@ | |
"webpack": "^4.6.0", | ||
"webpack-bundle-analyzer": "^2.9.2", | ||
"webpack-cli": "^2.0.9", | ||
"why-did-you-update": "^0.1.1", | ||
"webpack-serve": "^0.3.1" | ||
"webpack-serve": "^0.3.1", | ||
"why-did-you-update": "^0.1.1" | ||
}, | ||
"peerDependencies": { | ||
"react": "^15.0.0 || ^16.0.0" | ||
}, | ||
"ava": { | ||
"files": ["src/**/*.test.js"], | ||
"require": ["babel-register", "ignore-styles", "jsdom-global/register", "./setupEnzyme"], | ||
"files": [ | ||
"src/**/*.test.js" | ||
], | ||
"require": [ | ||
"babel-register", | ||
"ignore-styles", | ||
"jsdom-global/register", | ||
"./setupEnzyme" | ||
], | ||
"babel": { | ||
"testOptions": { | ||
"babelrc": false, | ||
"plugins": ["@babel/plugin-syntax-jsx"], | ||
"presets": ["@babel/preset-stage-3"] | ||
"plugins": [ | ||
"@babel/plugin-syntax-jsx" | ||
], | ||
"presets": [ | ||
"@babel/preset-stage-3" | ||
] | ||
} | ||
}, | ||
"snapshotDir": "__snapshots__" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Modified debounce that always invokes on leading edge | ||
* See unmodified: https://gist.github.com/mrchief/a7e8938ee96774f05644905b37f09536 | ||
*/ | ||
export default (func, wait) => { | ||
let timeout | ||
|
||
return (...args) => { | ||
const later = () => { | ||
timeout = null | ||
func(...args) | ||
} | ||
|
||
// timeout will be undefined the first time (leading edge) | ||
// so the callback will get executed once on leading edge | ||
const callNow = !timeout | ||
|
||
clearTimeout(timeout) | ||
timeout = setTimeout(later, wait) | ||
|
||
if (callNow) func(...args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import test from 'ava' | ||
|
||
import debounce from './debounce' | ||
|
||
test.cb("calls on leading edge and doesn't call until wait ends", t => { | ||
t.plan(2) | ||
const debounced = debounce(() => t.pass(), 50) | ||
debounced() | ||
debounced() | ||
debounced() | ||
setTimeout(() => { | ||
t.end() | ||
}, 70) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters