-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
130 additions
and
90 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,18 @@ | ||
import expect from 'expect'; | ||
import { timer } from '../time-utils'; | ||
|
||
describe('timer', () => { | ||
it('records how long a function takes to execute', () => { | ||
const add100k = (number) => { | ||
for (let i = 0; i < 100000; i += 1) { | ||
number += 1; | ||
} | ||
return number; | ||
}; | ||
|
||
const timedFn = timer(add100k); | ||
const result = timedFn(70); | ||
expect(result).toEqual(100070); | ||
expect(timedFn.time).toBeA('number'); | ||
}); | ||
}); |
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,26 @@ | ||
import moment from 'moment'; | ||
|
||
// Replacement for timely dependency | ||
export function timer(fn) { | ||
const timedFn = (...args) => { | ||
const start = new Date(); | ||
const result = fn.apply(fn, args); | ||
timedFn.time = new Date() - start; | ||
return result; | ||
}; | ||
return timedFn; | ||
} | ||
|
||
export function nowInSecondsPrecision() { | ||
return moment().startOf('second'); | ||
} | ||
|
||
export function clampToNowInSecondsPrecision(timestamp) { | ||
const now = nowInSecondsPrecision(); | ||
return timestamp.isAfter(now) ? now : timestamp; | ||
} | ||
|
||
// This is unfortunately not there in moment.js | ||
export function scaleDuration(duration, scale) { | ||
return moment.duration(duration.asMilliseconds() * scale); | ||
} |
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