@tanstack/virtual-core 3.14.0 → 3.15.0

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.
@@ -0,0 +1,44 @@
1
+ // Lazy materialization for the lanes===1 fast path. Backed by a
2
+ // Float64Array (stride 2: start, size, …); VirtualItems are constructed on
3
+ // first indexed read and cached. Saves the per-item object allocation at
4
+ // large list counts where most items are never visible.
5
+
6
+ import type { VirtualItem } from './index'
7
+
8
+ type Key = number | string | bigint
9
+
10
+ export function createLazyMeasurementsView(
11
+ count: number,
12
+ flat: Float64Array,
13
+ getItemKey: (i: number) => Key,
14
+ ): Array<VirtualItem> {
15
+ const cache: Array<VirtualItem | undefined> = new Array(count)
16
+ return new Proxy(cache as any, {
17
+ get(target, prop, receiver) {
18
+ if (typeof prop === 'string') {
19
+ // Cheap digit-prefix sniff before number coerce.
20
+ const c = prop.charCodeAt(0)
21
+ if (c >= 48 && c <= 57) {
22
+ const i = +prop
23
+ if (Number.isInteger(i) && i >= 0 && i < count) {
24
+ let v = target[i]
25
+ if (!v) {
26
+ const s = flat[i * 2]!
27
+ v = target[i] = {
28
+ index: i,
29
+ key: getItemKey(i),
30
+ start: s,
31
+ size: flat[i * 2 + 1]!,
32
+ end: s + flat[i * 2 + 1]!,
33
+ lane: 0,
34
+ }
35
+ }
36
+ return v
37
+ }
38
+ }
39
+ if (prop === 'length') return count
40
+ }
41
+ return Reflect.get(target, prop, receiver)
42
+ },
43
+ }) as Array<VirtualItem>
44
+ }
package/src/utils.ts CHANGED
@@ -18,8 +18,13 @@ export function memo<TDeps extends ReadonlyArray<any>, TResult>(
18
18
  let isInitial = true
19
19
 
20
20
  function memoizedFunction(): TResult {
21
- let depTime: number
22
- if (opts.key && opts.debug?.()) depTime = Date.now()
21
+ // Debug-only timing. In production builds, `process.env.NODE_ENV !==
22
+ // 'production'` is constant-folded to `false` by downstream minifiers
23
+ // (Terser/esbuild/swc with `define`), which DCEs the entire block.
24
+ const debugEnabled =
25
+ process.env.NODE_ENV !== 'production' && !!opts.key && !!opts.debug?.()
26
+ let depTime = 0
27
+ if (debugEnabled) depTime = Date.now()
23
28
 
24
29
  const newDeps = getDeps()
25
30
 
@@ -33,14 +38,14 @@ export function memo<TDeps extends ReadonlyArray<any>, TResult>(
33
38
 
34
39
  deps = newDeps
35
40
 
36
- let resultTime: number
37
- if (opts.key && opts.debug?.()) resultTime = Date.now()
41
+ let resultTime = 0
42
+ if (debugEnabled) resultTime = Date.now()
38
43
 
39
44
  result = fn(...newDeps)
40
45
 
41
- if (opts.key && opts.debug?.()) {
42
- const depEndTime = Math.round((Date.now() - depTime!) * 100) / 100
43
- const resultEndTime = Math.round((Date.now() - resultTime!) * 100) / 100
46
+ if (debugEnabled) {
47
+ const depEndTime = Math.round((Date.now() - depTime) * 100) / 100
48
+ const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100
44
49
  const resultFpsPercentage = resultEndTime / 16
45
50
 
46
51
  const pad = (str: number | string, num: number) => {