npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

viewport-helper

v0.0.3

Published

Performance-optimized viewport dimensions and element rect caching

Readme

viewport-helper

A performance-optimized TypeScript library for caching viewport dimensions and element rectangles to prevent layout reflows and improve web application performance.

[!WARNING] This library is still in early development and may not be suitable for production use. It is recommended to use it in a development environment and testing only. Before it reaches 1.0.0, it may undergo significant changes and deprecations.

[!NOTE] This library is designed for use in web applications and is not intended for use in server-side rendering or Node.js environments. It gracefully fails if it doesn't detect a window object.

Features

  • 🚀 Performance-optimized - Caches viewport units (lvh, svh) and element rects to avoid costly reflows
  • 📱 Mobile-friendly - Handles viewport changes on mobile devices without firing on scroll
  • Smart caching - Automatically invalidates caches on scroll, resize, or element changes
  • 🎯 TypeScript support - Full type safety with comprehensive TypeScript definitions
  • 🔄 Event-driven - Listen for meaningful viewport changes with custom events
  • 🧹 Memory-safe - Automatic cleanup of disconnected elements to prevent memory leaks
  • 📦 ESM/CJS support - Compatible with both ES Modules and CommonJS

Installation

npm install viewport-helper

Quick Start

import viewportHelper from "viewport-helper";

// Get cached viewport dimensions (no reflow!)
const height = viewportHelper.lvh(); // 100lvh in pixels
const smallHeight = viewportHelper.svh(50); // 50svh in pixels
const width = viewportHelper.innerWidth();

// Get all dimensions at once
const { lvh, svh, innerWidth, innerHeight } = viewportHelper.getAll();

// Track element rectangles efficiently
const element = document.querySelector(".my-element") as HTMLElement;
const rect = viewportHelper.rect(element); // Cached until element changes

// Listen for viewport changes
viewportHelper.addEventListener("resize", (event) => {
  console.log("Viewport changed:", event.detail);
});

API Reference

Viewport Dimensions

lvh(size?: number, round?: boolean): number

Gets the cached large viewport height (lvh) in pixels.

const fullHeight = viewportHelper.lvh(); // 100lvh (default)
const halfHeight = viewportHelper.lvh(50); // 50lvh
const preciseHeight = viewportHelper.lvh(100, false); // Unrounded

svh(size?: number, round?: boolean): number

Gets the cached small viewport height (svh) in pixels.

const fullHeight = viewportHelper.svh(); // 100svh (default)
const quarterHeight = viewportHelper.svh(25); // 25svh

innerWidth(): number

Gets the cached window.innerWidth value.

const width = viewportHelper.innerWidth();

innerHeight(): number

Gets the cached window.innerHeight value.

const height = viewportHelper.innerHeight();

getAll(): object

Gets all cached viewport dimensions at once.

const dimensions = viewportHelper.getAll();
// Returns: { lvh: number, svh: number, innerWidth: number, innerHeight: number }

Element Rectangle Caching

rect(element: HTMLElement): DOMRect

Gets the cached bounding rectangle for an element. Safe to call repeatedly in animation loops.

const element = document.querySelector(".box") as HTMLElement;

// First call measures and caches
const rect = viewportHelper.rect(element);

// Subsequent calls return cached values until invalidated
requestAnimationFrame(() => {
  const cachedRect = viewportHelper.rect(element); // No reflow!
  console.log(`Position: ${cachedRect.x}, ${cachedRect.y}`);
});

untrack(element: HTMLElement): void

Stops tracking an element's bounding rectangle and removes it from cache.

viewportHelper.untrack(element);

clearTracked(): void

Clears all tracked elements from the cache.

viewportHelper.clearTracked();

Events

resize Event

Fired when viewport dimensions actually change (not on mobile scroll).

viewportHelper.addEventListener("resize", (event) => {
  const { lvh, svh, innerWidth, innerHeight, changed } = event.detail;

  if (changed.lvh) {
    console.log("Large viewport height changed to:", lvh);
  }

  if (changed.svh) {
    console.log("Small viewport height changed to:", svh);
  }
});

Event Detail Interface:

interface ViewportHelperResizeDetail {
  lvh: number;
  svh: number;
  innerWidth: number;
  innerHeight: number;
  changed: {
    lvh: boolean;
    svh: boolean;
    innerWidth: boolean;
    innerHeight: boolean;
  };
}

Cleanup

destroy(): void

Cleans up all resources used by the size manager.

// Call during app teardown
viewportHelper.destroy();

Use Cases

1. Performance-Critical Animations

import viewportHelper from "viewport-helper";

const animateElement = (element: HTMLElement) => {
  requestAnimationFrame(() => {
    // No layout reflow - values are cached!
    const rect = viewportHelper.rect(element);
    const viewportHeight = viewportHelper.lvh();

    // Perform calculations safely
    if (rect.top > viewportHeight) {
      element.style.transform = "translateY(-100%)";
    }
  });
};

2. Responsive Design with Viewport Units

import viewportHelper from "viewport-helper";

const updateLayout = () => {
  const isMobile = viewportHelper.innerWidth() < 768;
  const height = isMobile ? viewportHelper.svh() : viewportHelper.lvh();

  document.documentElement.style.setProperty(
    "--viewport-height",
    `${height}px`,
  );
};

viewportHelper.addEventListener("resize", updateLayout);
updateLayout(); // Initial setup

3. Scroll-Based Effects

import viewportHelper from "viewport-helper";

const handleScroll = () => {
  document.querySelectorAll(".parallax").forEach((element) => {
    // Cached rect - no performance penalty!
    const rect = viewportHelper.rect(element);
    const progress = rect.top / viewportHelper.innerHeight();

    element.style.transform = `translateY(${progress * 50}px)`;
  });
};

window.addEventListener("scroll", handleScroll, { passive: true });

4. Dynamic Content Layout

import viewportHelper from "viewport-helper";

const positionModal = (modal: HTMLElement) => {
  const modalRect = viewportHelper.rect(modal);
  const viewportHeight = viewportHelper.lvh();
  const viewportWidth = viewportHelper.innerWidth();

  // Center modal without causing reflow
  modal.style.left = `${(viewportWidth - modalRect.width) / 2}px`;
  modal.style.top = `${(viewportHeight - modalRect.height) / 2}px`;
};

Browser Support

  • Chrome/Edge 88+ (ResizeObserver, lvh/svh units)
  • Firefox 89+
  • Safari 13.1+

Performance Benefits

  • Eliminates layout reflows when accessing viewport dimensions
  • Caches element rectangles until they actually change
  • Batches measurements using RequestAnimationFrame
  • Automatic cleanup of disconnected DOM elements
  • Smart invalidation only when necessary

License

MIT © 3dfactor