Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Performance and complexity: fixed-height list virtualization

Distinguish actual execution time from how cost grows with data size. Fixed-height list virtualization calculates the visible range and limits rendered elements, but does not automatically reduce the cost of storing or processing the entire dataset.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const totalHeight = count * rowHeight;
const maxScroll = Math.max(0, totalHeight - viewportHeight);
const top = Math.min(Math.max(0, scrollTop), maxScroll);
const start = Math.max(0, Math.floor(top / rowHeight) - overscan);
const end = Math.min(count, Math.ceil((top + viewportHeight) / rowHeight) + overscan);
const offset = start * rowHeight;

Performance includes execution time, memory use, and responsiveness. Complexity describes how cost grows with input size: O(1) means cost bounded independently of that size, while O(n) means cost growing at most linearly. This is an analysis model, not a guarantee of milliseconds. ECMAScript specifies language behavior; it does not specify interface rendering costs. Before optimizing, measure representative workloads in the target environment.

Fixed-height list virtualization means rendering only the visible portion of a list plus an optional overscan buffer. For count rows of identical height rowHeight, the full height is count * rowHeight. The example calculates the half-open range [start, end), its offset, and totalHeight, accounting for partially visible rows. It clamps scrollTop to the valid scrolling range.

Assuming constant-cost numeric operations, range calculation takes O(1) time and extra memory. Rendering k = end - start rows requires work proportional to k if each row has a constant rendering cost. Storing all n records still takes O(n) memory; filtering the entire dataset on every scroll defeats this computational benefit.

This is a calculation example, not a complete DOM component. An integration should preserve a scrollable area of height totalHeight and position the range at offset. The actual row pitch, including spacing, must match rowHeight; variable heights require a different algorithm. Focus, accessibility, and data changes also need handling. The example uses ES2015 syntax and requires no browser APIs; Node 24 in a repository does not establish browser support for those APIs.

Examples

function visibleRange(count, rowHeight, viewportHeight, scrollTop, overscan) {
  // Preconditions: all arguments and count * rowHeight are safe integers.
  // count and overscan are nonnegative; rowHeight and viewportHeight are positive.
  // Heights and scrollTop use consistent units.
  const totalHeight = count * rowHeight;
  const maxScroll = Math.max(0, totalHeight - viewportHeight);
  const top = Math.min(Math.max(0, scrollTop), maxScroll);
  const start = Math.max(0, Math.floor(top / rowHeight) - overscan);
  const end = Math.min(count, Math.ceil((top + viewportHeight) / rowHeight) + overscan);
  return { start, end, offset: start * rowHeight, totalHeight };
}

console.log(JSON.stringify(visibleRange(100, 20, 40, 45, 1)));
console.log(JSON.stringify(visibleRange(0, 20, 40, 0, 1)));

Result

{"start":1,"end":6,"offset":20,"totalHeight":2000}
{"start":0,"end":0,"offset":0,"totalHeight":0}

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources