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

image_compressor_wasm

v1.0.0

Published

A fast WebAssembly image compression library for JavaScript, React, and Next.js

Readme

Image Compressor WASM

A fast image compression library powered by WebAssembly.

Image Compressor WASM provides a simple API for converting and compressing images into JPEG or PNG format. It can be used in JavaScript, TypeScript, React, Next.js, and supported Node.js environments.

Features

  • Fast image processing powered by WebAssembly
  • JPEG output support
  • PNG output support
  • Simple Uint8Array API
  • Compatible with JavaScript and TypeScript
  • Suitable for React and Next.js applications
  • Can be used for client-side image compression
  • Includes TypeScript declarations

Installation

Install the package using npm:

npm install image_compressor_wasm

Using pnpm:

pnpm add image_compressor_wasm

Using Yarn:

yarn add image_compressor_wasm

Basic Usage

Import the compression functions from the package:

import {
  compress_to_jpeg,
  compress_to_png
} from 'image_compressor_wasm';

const input = new Uint8Array(imageData);

const jpegOutput = compress_to_jpeg(input);
const pngOutput = compress_to_png(input);

Both functions accept a Uint8Array and return the compressed image as a new Uint8Array.

Node.js Usage

The following example reads an image, converts it to JPEG and PNG, and writes the results to the file system.

import fs from 'node:fs';

import {
  compress_to_jpeg,
  compress_to_png
} from 'image_compressor_wasm';

function readImage(filePath) {
  const buffer = fs.readFileSync(filePath);
  return new Uint8Array(buffer);
}

function saveImage(imageData, outputPath) {
  const buffer = Buffer.from(imageData);
  fs.writeFileSync(outputPath, buffer);
}

const input = readImage('input.jpg');

const jpegStartTime = performance.now();
const jpegOutput = compress_to_jpeg(input);
const jpegDuration = performance.now() - jpegStartTime;

saveImage(jpegOutput, 'photo.jpg');

console.log(`JPEG compression completed in ${jpegDuration.toFixed(2)} ms`);
console.log(`JPEG output size: ${jpegOutput.byteLength} bytes`);

const pngStartTime = performance.now();
const pngOutput = compress_to_png(input);
const pngDuration = performance.now() - pngStartTime;

saveImage(pngOutput, 'photo.png');

console.log(`PNG compression completed in ${pngDuration.toFixed(2)} ms`);
console.log(`PNG output size: ${pngOutput.byteLength} bytes`);

Because this package is distributed as an ES module, use import instead of require.

Browser Usage

Use File.arrayBuffer() to read a selected image as a Uint8Array.

import {
  compress_to_jpeg
} from 'image_compressor_wasm';

async function compressImage(file) {
  const inputBuffer = await file.arrayBuffer();
  const input = new Uint8Array(inputBuffer);

  const output = compress_to_jpeg(input);

  return new Blob([output], {
type: 'image/jpeg'
  });
}

Create an object URL to preview the compressed image:

const compressedBlob = await compressImage(file);
const previewUrl = URL.createObjectURL(compressedBlob);

console.log(previewUrl);

Revoke the object URL when it is no longer needed:
URL.revokeObjectURL(previewUrl);

React Usage

The following component allows the user to select an image, compresses it to JPEG, and displays a preview.

import { useEffect, useState } from 'react';

import {
  compress_to_jpeg
} from 'image_compressor_wasm';

export default function ImageCompressor() {
  const [previewUrl, setPreviewUrl] = useState('');
  const [originalSize, setOriginalSize] = useState(0);
  const [compressedSize, setCompressedSize] = useState(0);
  const [processing, setProcessing] = useState(false);

  useEffect(() => {
return () => {
if (previewUrl) {
URL.revokeObjectURL(previewUrl);
}
};
  }, [previewUrl]);

  async function handleFileChange(event) {
const file = event.target.files?.[0];

if (!file) {
return;
}

setProcessing(true);

try {
const inputBuffer = await file.arrayBuffer();
const input = new Uint8Array(inputBuffer);
const output = compress_to_jpeg(input);

const compressedBlob = new Blob([output], {
type: 'image/jpeg'
});

const nextPreviewUrl = URL.createObjectURL(compressedBlob);

setPreviewUrl((currentPreviewUrl) => {
if (currentPreviewUrl) {
URL.revokeObjectURL(currentPreviewUrl);
}

return nextPreviewUrl;
});

setOriginalSize(file.size);
setCompressedSize(compressedBlob.size);
} catch (error) {
console.error('Image compression failed:', error);
} finally {
setProcessing(false);
}
  }

  return (
<div>
<input
type="file"
accept="image/jpeg,image/png"
disabled={processing}
onChange={handleFileChange}
/>

{processing && <p>Compressing image...</p>}

{previewUrl && (
<div>
<p>Original size: {originalSize} bytes</p>
<p>Compressed size: {compressedSize} bytes</p>

<img
src={previewUrl}
alt="Compressed preview"
style={{ maxWidth: '100%' }}
/>
</div>
)}
</div>
  );
}

Next.js Usage

When using the package inside a Next.js App Router project, compression should run in a Client Component.

'use client';

import { useState } from 'react';

import {
  compress_to_jpeg
} from 'image_compressor_wasm';

export default function ImageCompressor() {
  const [compressedFile, setCompressedFile] = useState(null);
  const [processing, setProcessing] = useState(false);

  async function handleFileChange(event) {
const file = event.target.files?.[0];

if (!file) {
return;
}

setProcessing(true);

try {
const inputBuffer = await file.arrayBuffer();
const input = new Uint8Array(inputBuffer);
const output = compress_to_jpeg(input);

const result = new File(
[output],
'compressed.jpg',
{
type: 'image/jpeg',
lastModified: Date.now()
}
);

setCompressedFile(result);
} catch (error) {
console.error('Image compression failed:', error);
} finally {
setProcessing(false);
}
  }

  return (
<div>
<input
type="file"
accept="image/jpeg,image/png"
disabled={processing}
onChange={handleFileChange}
/>

{processing && <p>Compressing image...</p>}

{compressedFile && (
<p>
Compressed file size: {compressedFile.size} bytes
</p>
)}
</div>
  );
}

Uploading a Compressed Image

The compressed output can be converted to a File and uploaded using FormData.

import {
  compress_to_jpeg
} from 'image_compressor_wasm';

async function compressAndUpload(file) {
  const inputBuffer = await file.arrayBuffer();
  const input = new Uint8Array(inputBuffer);
  const output = compress_to_jpeg(input);

  const compressedFile = new File(
[output],
'compressed.jpg',
{
type: 'image/jpeg',
lastModified: Date.now()
}
  );

  const formData = new FormData();
  formData.append('image', compressedFile);

  const response = await fetch('/api/upload', {
method: 'POST',
body: formData
  });

  if (!response.ok) {
throw new Error(`Upload failed with status ${response.status}`);
  }

  return response.json();
}

Downloading a Compressed Image

Use an object URL to download the result directly