-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize the HoursTransformer (#784)
Improve the HoursTransformer The clonedeep unnecessarily increased the computation time. Removing it improves performance by about 22%. This PR also improves the clarity of the code by making it more functional and making the data models easier to understand. J=SLAP-1289 TEST=manual, auto Smoke test the openStatus formatter and the hoursList formatter which both rely on this code. Add unit tests. Benchmark the code by running the function 1000 times and comparing the time in milliseconds before and after.
- Loading branch information
Showing
2 changed files
with
204 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { DayNames } from '../../../../static/js/hours/constants'; | ||
import HoursTransformer from '../../../../static/js/hours/transformer'; | ||
|
||
const dateClass = Date; | ||
const mockDateClass = class extends Date { | ||
constructor(date = 'July 21, 2020 12:42:00 GMT-0500') { | ||
super(date); | ||
} | ||
}; | ||
|
||
describe('formats hours for answers', () => { | ||
beforeEach(() => { | ||
// Mock the date class because holiday hours are only applied if they are for | ||
// dates which are within one week of the current date. | ||
global.Date = mockDateClass; | ||
}); | ||
|
||
afterEach(() => { | ||
global.Date = dateClass; | ||
}) | ||
|
||
it('formats hours for multiple days', () => { | ||
const expectedFormattedHours = [ | ||
{ | ||
day: DayNames.MONDAY, | ||
intervals: [{end: 400, start: 100}], | ||
isClosed: false, | ||
openIntervals: [{end: 400, start: 100}], | ||
}, | ||
{ | ||
day: DayNames.TUESDAY, | ||
intervals: [{end: 800, start: 100}], | ||
openIntervals: [{end: 800, start: 100}], | ||
isClosed: false | ||
}, | ||
{ | ||
day: DayNames.WEDNESDAY, | ||
intervals: [], | ||
openIntervals: [], | ||
isClosed: true | ||
} | ||
]; | ||
const days = { | ||
monday: { | ||
isClosed: false, | ||
openIntervals: [{ start: '01:00', end: '04:00' }] | ||
}, | ||
tuesday: { | ||
isClosed: false, | ||
openIntervals: [{ start: '01:00', end: '08:00' }] | ||
}, | ||
wednesday: { | ||
isClosed: true | ||
} | ||
} | ||
const timezoneOffset = '-04:00'; | ||
const actualFormattedHours = HoursTransformer._formatHoursForAnswers(days, timezoneOffset); | ||
expect(actualFormattedHours).toEqual(expectedFormattedHours); | ||
}); | ||
|
||
it('formats hours for multiple days and holiday hours', () => { | ||
const expectedFormattedHours = [ | ||
{ | ||
day: DayNames.TUESDAY, | ||
intervals: [{end: 800, start: 100}], | ||
openIntervals: [{end: 800, start: 100}], | ||
isClosed: false, | ||
dailyHolidayHours: { | ||
date: '2020-07-21', | ||
intervals: [{end: 200, start: 100}], | ||
openIntervals: [ | ||
{ | ||
end: 200, | ||
start: 100, | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
day: DayNames.WEDNESDAY, | ||
intervals: [{end: 1000, start: 100}], | ||
openIntervals: [{end: 1000, start: 100}], | ||
isClosed: false, | ||
dailyHolidayHours: { | ||
date: '2020-07-22', | ||
isClosed: true, | ||
intervals: [], | ||
openIntervals: [], | ||
}, | ||
}, | ||
{ | ||
day: DayNames.THURSDAY, | ||
intervals: [{end: 400, start: 100}], | ||
isClosed: false, | ||
openIntervals: [{end: 400, start: 100}], | ||
dailyHolidayHours: { | ||
date: '2020-07-23', | ||
intervals: [], | ||
openIntervals: [], | ||
isRegularHours: true | ||
}, | ||
}, | ||
]; | ||
const days = { | ||
tuesday: { | ||
isClosed: false, | ||
openIntervals: [{ start: '01:00', end: '08:00' }] | ||
}, | ||
wednesday: { | ||
isClosed: false, | ||
openIntervals: [{ start: '01:00', end: '10:00' }] | ||
}, | ||
thursday: { | ||
isClosed: false, | ||
openIntervals: [{ start: '01:00', end: '04:00' }] | ||
}, | ||
holidayHours: [ | ||
{ date: '2020-07-21', openIntervals: [{ start: '01:00', end: '02:00' }] }, | ||
{ date: '2020-07-22', isClosed: true }, | ||
{ date: '2020-07-23', isRegularHours: true } | ||
] | ||
} | ||
const timezoneOffset = '-04:00'; | ||
const actualFormattedHours = HoursTransformer._formatHoursForAnswers(days, timezoneOffset); | ||
expect(actualFormattedHours).toEqual(expectedFormattedHours); | ||
}); | ||
}); |