Skip to content

Commit

Permalink
perf: optimise measurements calculation (#17)
Browse files Browse the repository at this point in the history
* optimise measurements calculation

* remove additinal const declaration
  • Loading branch information
Arjun authored May 10, 2020
1 parent f3f38f3 commit fc3c460
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,23 @@ export function useVirtual({
mountedRef.current = true
}, [estimateSize, size])

const measurements = React.useMemo(() => {
const { measurements, reversedMeasurements } = React.useMemo(() => {
const measurements = []
for (let i = 0; i < size; i++) {
const reversedMeasurements = []

for (let i = 0, j = size - 1; i < size; i++, j--) {
const start = measurements[i - 1]?.end || 0
const size = measuredCache[i] || estimateSize(i)
const end = start + size
const bounds = { index: i, start, size, end }
measurements[i] = {
index: i,
start,
size,
end,
...bounds,
}
reversedMeasurements[j] = {
...bounds,
}
}

return measurements
return { measurements, reversedMeasurements }
}, [estimateSize, measuredCache, size])

const totalSize = measurements[size - 1]?.end || 0
Expand All @@ -72,9 +74,9 @@ export function useVirtual({
)
let end = React.useMemo(
() =>
[...measurements]
.reverse()
.find(rowStat => rowStat.start <= scrollOffsetPlusOuterSize),
reversedMeasurements.find(
rowStat => rowStat.start <= scrollOffsetPlusOuterSize
),
[measurements, scrollOffsetPlusOuterSize]
)

Expand Down

0 comments on commit fc3c460

Please sign in to comment.