Docs

Slim

Compression suite for javascript objects

Motivation

Slim is a compression suite for javascript objects. It is designed to be used in the browser and in Node.js envs. The main goal of this package is to improve performance by reducing the size of the data being sent over the wire.

We've encountered many problems with URL link length when passing filters in params and the size of the data we send to the server. We've tried many libraries and none of them were good enough for our needs. So we decided to create our own library based on PAKO zlib port.

Demo

Compression demo - Rescale Tools

Check out the compression demo on Rescale Tools.

Installation

Terminal
npm install @rescale/slim

Usage

/app/api/data/route.ts
import { type NextRequest, NextResponse } from 'next/server';
import { inflate, deflate } from '@rescale/slim';
 
export function GET(request: NextRequest) {
  const compressed = request.nextUrl.searchParams.get('compressed');
 
  return NextResponse.json({
    decompressed: inflate(compressed),
  });
}
/app/page.tsx
import { inflate, deflate } from '@rescale/slim';
 
const data = { a: 1, b: 2, c: 3 };
 
const compressed = deflate(data);
 
const _decompressed = inflate(compressed);
 
export default async function Page() {
  const { decompressed } = await fetch(
    `/api/data?compressed=${compressed}`,
  ).then((res) => res.json());
 
  return (
    <div>
      <h1>Compressed data</h1>
      <pre>{compressed}</pre>
      <h1>compatibility</h1>
      <pre>
        {String(JSON.stringify(_decompressed) === JSON.stringify(decompressed))}
      </pre>
    </div>
  );
}

Last updated on

On this page