-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hidden delta to FPS calculation 🍯
- Loading branch information
Showing
4 changed files
with
90 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,4 @@ | |
"bugs": { | ||
"url": "https://github.com/pirelenito/collect-fps/issues" | ||
} | ||
} | ||
} |
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,35 @@ | ||
export default (frames, visibilityEvents) => | ||
1000 / ( | ||
( | ||
(frames[frames.length - 1] - frames[0]) - | ||
getHiddenDelta(visibilityEvents) | ||
) / | ||
(frames.length - 1) | ||
) | ||
|
||
export const getHiddenDelta = (visibilityEvents) => | ||
visibilityEvents | ||
.reduce((deltas, [visible, timestamp]) => { | ||
const last = deltas.slice(-1)[0] | ||
const remaining = deltas.slice(0, -1) | ||
if (visible && !last) { return deltas } | ||
|
||
if (!visible) { | ||
return [ | ||
...deltas, | ||
[timestamp] | ||
] | ||
} else { | ||
return [ | ||
...remaining, | ||
[last[0], timestamp] | ||
] | ||
} | ||
}, []) | ||
.reduce( | ||
(delta, [start, end]) => | ||
end | ||
? delta + (end - start) | ||
: delta | ||
, 0 | ||
) |
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,42 @@ | ||
import calculateFPS, { getHiddenDelta } from './calculate-fps' | ||
|
||
describe('calculate-fps', () => { | ||
describe('calculateFPS', () => { | ||
it('should calculate the fps', () => { | ||
const frames = [200, 400, 600, 800, 1000] | ||
|
||
expect(calculateFPS(frames, [])).toBe(5) | ||
}) | ||
|
||
it('should calculate the fps taking into account the hidden delta', () => { | ||
const frames = [200, 400, 600, 800, 1000] | ||
const visibilityEvents = [[false, 0], [true, 300]] | ||
|
||
expect(calculateFPS(frames, visibilityEvents)).toBe(8) | ||
}) | ||
}) | ||
|
||
describe('getHiddenDelta', () => { | ||
it('should calculate the amount of time that it spent hidden', () => { | ||
const visibilityEvents = [ | ||
[false, 1000], | ||
[true, 1010], | ||
[false, 1100], | ||
[true, 1200] | ||
] | ||
|
||
expect(getHiddenDelta(visibilityEvents)).toBe(110) | ||
}) | ||
|
||
it('should ignore unfinished hidden sequences', () => { | ||
const visibilityEvents = [ | ||
[true, 100], | ||
[false, 1000], | ||
[true, 1010], | ||
[false, 1100] | ||
] | ||
|
||
expect(getHiddenDelta(visibilityEvents)).toBe(10) | ||
}) | ||
}) | ||
}) |
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