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

@zeitfall/webgpu-exclusive-scan

v1.0.1

Published

A minimal, high-performance WebGPU library implementing an in-place exclusive prefix sum (scan). Designed for GPU-accelerated rendering and compute pipelines, it leverages hardware subgroup operations to maximize throughput, minimize global memory roundtr

Readme

Overview

A minimal, high-performance WebGPU library implementing an in-place exclusive prefix sum (scan). Designed for GPU-accelerated rendering and compute pipelines, it leverages hardware subgroup operations to maximize throughput, minimize global memory roundtrips, and efficiently process contiguous arrays of 32-bit unsigned integers (Uint32Array).

Requirements: WebGPU-enabled browser with subgroups feature and required WGSL extensions (linear_indexing, subgroup_uniformity, subgroup_id).

Installation: npm install @zeitfall/webgpu-exclusive-scan


Mathematical & Algorithmic Properties

| Operation / Metric | WebGPUExclusiveScanner | | --- | --- | | Work Complexity | O(n) | | Step (Time) Complexity | O(log n) | | Auxiliary Space | O(n / k) |

Note 1: n represents the total number of scalar elements within the dataset buffer.

Note 2: k represents the data processed per workgroup block (4 * workgroupSize). Auxiliary space is dynamically allocated for intermediate block sums across hierarchical compute passes.


API Reference

WebGPUExclusiveScanner

The primary controller class managing the compute pipelines and dispatch configurations for the parallel scan.

  • Constructor constructor(gpuDevice: GPUDevice, workgroupSize = 64) Initializes the internal compute pipelines and shader modules. The gpuDevice must be instantiated with the subgroups feature enabled. Throws an error if subgroups are unsupported.
  • scan(encoder: GPUCommandEncoder, dataBuffer: GPUBuffer, dataLength: number): void Dispatches the scan and uniform-add compute passes. Mutates the dataBuffer in-place. The target buffer must be created with the GPUBufferUsage.STORAGE flag.
  • dispose(dataBuffer: GPUBuffer): void Frees the internally cached intermediate block buffers associated with a specific dataBuffer. Must be called to prevent memory leaks when the primary data buffer is destroyed by the host.

Operational Context (Behavioral Notes)

  • Subgroup Intrinsics: Bypasses workgroup shared memory overhead by utilizing subgroupExclusiveAdd and subgroupAdd for intra-wavefront reductions, drastically reducing shared memory synchronization barriers and increasing execution speed.
  • In-Place Mutation: The algorithm updates the input GPUBuffer directly without requiring a secondary destination buffer. This eliminates the need for VRAM-heavy Ping-Pong buffer allocations during the main scan passes.
  • Zero-Allocation Execution: Intermediate block sum buffers are dynamically allocated upon the first scan and retained in an internal WeakMap. Subsequent scans on the same GPUBuffer execute with zero allocation overhead.
  • Vectorized Loads/Stores: Memory access is strictly optimized via 128-bit vec4u coalesced reads and writes (array<vec4u>), saturating memory bandwidth and minimizing cache misses across the global invocation grid.

Usage Example

import { WebGPUExclusiveScanner } from '@zeitfall/webgpu-exclusive-scan';

const gpuDevice = await initGPUDevice();
const scanner = new WebGPUExclusiveScanner(gpuDevice);

// Generate mock data for the scan
const inputData = new Uint32Array(8_000_000).map(() => Math.ceil(16 * Math.random()));

const inputBuffer = gpuDevice.createBuffer({
    size: inputData.byteLength,
    usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST
});

const resultBuffer = gpuDevice.createBuffer({
    size: inputData.byteLength,
    usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
});

// Upload data to VRAM
gpuDevice.queue.writeBuffer(inputBuffer, 0, inputData);

const commandEncoder = gpuDevice.createCommandEncoder();

// Perform the exclusive scan in-place
scanner.scan(commandEncoder, inputBuffer, inputData.length);
commandEncoder.copyBufferToBuffer(inputBuffer, resultBuffer);

gpuDevice.queue.submit([commandEncoder.finish()]);
await gpuDevice.queue.onSubmittedWorkDone();

// Read back the results
const resultArrayBuffer = await mapBuffer(resultBuffer);
const resultArray = new Uint32Array(resultArrayBuffer);

console.log('Input data:', inputData);
console.log('Prefix scan:', resultArray);

// --- Utility Functions ---

async function mapBuffer(buffer: GPUBuffer) {
    await buffer.mapAsync(GPUMapMode.READ);
    const arrayBuffer = buffer.getMappedRange().slice(0);
    buffer.unmap();
    return arrayBuffer;
}

async function initGPUDevice() {
    if (!navigator.gpu) throw new TypeError('WebGPU is not supported.');

    const gpuWGSLFeatues = navigator.gpu.wgslLanguageFeatures;
    const requiredFeatures = ['linear_indexing', 'subgroup_uniformity', 'subgroup_id'];

    requiredFeatures.forEach((feature) => {
        if (!gpuWGSLFeatues.has(feature)) {
            throw new TypeError(`GPU lacks required WGSL feature: "${feature}"`);
        }
    });

    const adapter = await navigator.gpu.requestAdapter();
    if (!adapter) throw new TypeError('No GPU adapter found.');
    if (!adapter.features.has('subgroups')) throw new TypeError('GPU lacks "subgroups" support.');

    return adapter.requestDevice({
        requiredFeatures: ['subgroups'],
        requiredLimits: {
            maxBufferSize: adapter.limits.maxBufferSize,
            maxComputeWorkgroupsPerDimension: adapter.limits.maxComputeWorkgroupsPerDimension
        }
    });
}

References

[1] Harris, M., Sengupta, S., & Owens, J. D. (2007). Parallel Prefix Sum (Scan) with CUDA. GPU Gems 3, Part VI, Chapter 39. NVIDIA Developer

[2] Yayo1. (2024). WebGPU Prefix Sum. yayo1.com