@tanstack/virtual-core 3.17.2 → 3.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -816,10 +816,17 @@ export class Virtualizer<
816
816
  this._intendedScrollOffset = null
817
817
 
818
818
  this.scrollAdjustments = 0
819
+ // If the offset hasn't moved, this is the echo of our own
820
+ // adjustment write — `applyScrollAdjustment` already folded it
821
+ // into `scrollOffset`. There's no direction to infer, so leave
822
+ // it alone; a real gesture always moves the offset.
823
+ const prevOffset = this.getScrollOffset()
819
824
  this.scrollDirection = isScrolling
820
- ? this.getScrollOffset() < offset
821
- ? 'forward'
822
- : 'backward'
825
+ ? prevOffset === offset
826
+ ? this.scrollDirection
827
+ : prevOffset < offset
828
+ ? 'forward'
829
+ : 'backward'
823
830
  : null
824
831
  this.scrollOffset = offset
825
832
  this.isScrolling = isScrolling
@@ -1362,22 +1369,22 @@ export class Virtualizer<
1362
1369
  this.options.lanes,
1363
1370
  ],
1364
1371
  (measurements, outerSize, scrollOffset, lanes) => {
1365
- return (this.range =
1366
- measurements.length > 0 && outerSize > 0
1367
- ? calculateRange({
1368
- measurements,
1369
- outerSize,
1370
- scrollOffset,
1371
- lanes,
1372
- // Pass the typed array so binary search + forward-walk can
1373
- // read start/end directly from Float64Array, skipping the
1374
- // Proxy traps that materialize a full VirtualItem per probe.
1375
- flat:
1376
- lanes === 1 && this._flatMeasurements != null
1377
- ? this._flatMeasurements
1378
- : null,
1379
- })
1380
- : null)
1372
+ if (measurements.length === 0 || outerSize === 0) {
1373
+ this.range = null
1374
+ return null
1375
+ }
1376
+ this.range = calculateRangeImpl(
1377
+ measurements,
1378
+ outerSize,
1379
+ scrollOffset,
1380
+ lanes,
1381
+ // Pass the typed array so binary search + forward-walk can read
1382
+ // start/end directly from Float64Array, skipping the Proxy traps.
1383
+ lanes === 1 && this._flatMeasurements != null
1384
+ ? this._flatMeasurements
1385
+ : null,
1386
+ )
1387
+ return this.range
1381
1388
  },
1382
1389
  {
1383
1390
  key: process.env.NODE_ENV !== 'production' && 'calculateRange',
@@ -1923,45 +1930,71 @@ const findNearestBinarySearch = (
1923
1930
  }
1924
1931
  }
1925
1932
 
1926
- function calculateRange({
1927
- measurements,
1928
- outerSize,
1929
- scrollOffset,
1930
- lanes,
1931
- flat,
1932
- }: {
1933
- measurements: Array<VirtualItem>
1934
- outerSize: number
1935
- scrollOffset: number
1936
- lanes: number
1937
- flat: Float64Array | null
1938
- }) {
1933
+ // Monomorphic Float64Array variant — reads start values directly at stride
1934
+ // 2 instead of through a getter closure. JITs the inner load to a typed-
1935
+ // array bounds-check + load with no indirect call.
1936
+ function findNearestBinarySearchFlat(
1937
+ flat: Float64Array,
1938
+ high: number,
1939
+ value: number,
1940
+ ) {
1941
+ let low = 0
1942
+ while (low <= high) {
1943
+ const middle = ((low + high) / 2) | 0
1944
+ const currentValue = flat[middle * 2]!
1945
+
1946
+ if (currentValue < value) {
1947
+ low = middle + 1
1948
+ } else if (currentValue > value) {
1949
+ high = middle - 1
1950
+ } else {
1951
+ return middle
1952
+ }
1953
+ }
1954
+ return low > 0 ? low - 1 : 0
1955
+ }
1956
+
1957
+ function calculateRangeImpl(
1958
+ measurements: Array<VirtualItem>,
1959
+ outerSize: number,
1960
+ scrollOffset: number,
1961
+ lanes: number,
1962
+ flat: Float64Array | null,
1963
+ ) {
1939
1964
  const lastIndex = measurements.length - 1
1940
- // When the lanes===1 fast-path is active, read start/end directly from the
1941
- // flat Float64Array instead of going through the lazy-view Proxy. Cuts
1942
- // ~17 Proxy.get traps per scroll for the binary search alone.
1943
- const getStart = flat
1944
- ? (index: number) => flat[index * 2]!
1945
- : (index: number) => measurements[index]!.start
1946
- const getEnd = flat
1947
- ? (index: number) => flat[index * 2]! + flat[index * 2 + 1]!
1948
- : (index: number) => measurements[index]!.end
1949
1965
 
1950
1966
  // handle case when item count is less than or equal to lanes
1951
1967
  if (measurements.length <= lanes) {
1952
- return {
1953
- startIndex: 0,
1954
- endIndex: lastIndex,
1968
+ return { startIndex: 0, endIndex: lastIndex }
1969
+ }
1970
+
1971
+ if (lanes === 1 && flat !== null) {
1972
+ // Hot single-lane path: typed-array reads, no closures, no Proxy traps.
1973
+ const startIndex = findNearestBinarySearchFlat(
1974
+ flat,
1975
+ lastIndex,
1976
+ scrollOffset,
1977
+ )
1978
+ let endIndex = startIndex
1979
+ const limit = scrollOffset + outerSize
1980
+ while (
1981
+ endIndex < lastIndex &&
1982
+ flat[endIndex * 2]! + flat[endIndex * 2 + 1]! < limit
1983
+ ) {
1984
+ endIndex++
1955
1985
  }
1986
+ return { startIndex, endIndex }
1956
1987
  }
1957
1988
 
1989
+ // Fallback (lanes > 1 or no flat array): closure-based reads.
1990
+ const getStart = (index: number) => measurements[index]!.start
1958
1991
  let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset)
1959
1992
  let endIndex = startIndex
1960
1993
 
1961
1994
  if (lanes === 1) {
1962
1995
  while (
1963
1996
  endIndex < lastIndex &&
1964
- getEnd(endIndex) < scrollOffset + outerSize
1997
+ measurements[endIndex]!.end < scrollOffset + outerSize
1965
1998
  ) {
1966
1999
  endIndex++
1967
2000
  }