Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache some moment instances when rebuilding modifiers
When modifiers need to be rebuilt, which happens pretty frequently, we iterate over every day and create a new moment instance for these operations. These moment instances are never mutated, so we can safely store and reuse them to improve performance. This optimization reduces the time spent in DayPickerRangeController#componentWillReceiveProps when selecting dates from ~16ms to ~6-10ms.
- Loading branch information
Showing
4 changed files
with
38 additions
and
2 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
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,10 @@ | ||
import moment from 'moment'; | ||
|
||
const momentPool = new Map(); | ||
export default function getPooledMoment(dayString) { | ||
if (!momentPool.has(dayString)) { | ||
momentPool.set(dayString, moment(dayString)); | ||
} | ||
|
||
return momentPool.get(dayString); | ||
} |
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,24 @@ | ||
import { expect } from 'chai'; | ||
import moment from 'moment'; | ||
|
||
import getPooledMoment from '../../src/utils/getPooledMoment'; | ||
|
||
describe('getPooledMoment', () => { | ||
it('returns a moment given a day string', () => { | ||
const momentObj = getPooledMoment('2017-12-10'); | ||
expect(moment.isMoment(momentObj)).to.equal(true); | ||
expect(momentObj.format('YYYY MM DD')).to.equal('2017 12 10'); | ||
}); | ||
|
||
it('returns the same moment given the same day string', () => { | ||
const momentObj1 = getPooledMoment('2017-12-10'); | ||
const momentObj2 = getPooledMoment('2017-12-10'); | ||
expect(momentObj1).to.equal(momentObj2); | ||
}); | ||
|
||
it('returns a different moment given a different day string', () => { | ||
const momentObj1 = getPooledMoment('2017-12-10'); | ||
const momentObj2 = getPooledMoment('2017-12-11'); | ||
expect(momentObj1).not.to.equal(momentObj2); | ||
}); | ||
}); |