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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sdgindex/compress

v1.0.0

Published

Compress and decompress objects and arrays

Downloads

2

Readme

@sdgindex/compress

The @sdgindex/compress package efficiently compresses JavaScript objects.

Installation

npm install @sdgindex/compress

How to use

import { compress, decompress } from "@sdgindex/compress";

const templates = [];

const users = [
  { id: 3, name: "Finn", age: 28 },
  { id: 7, name: "Mary", age: 47 },
  { id: 8, name: "Harry", location: "Paris" },
];

// Compress users
const cUsers = users.map((user) => compress(user, templates));

console.log(cUsers); // [[0, 3, "Finn", 28], [0, 7, "Mary", 47], [1, 8, "Harry", "Paris"]]
console.log(templates); // [["id", "name", "age"], ["id", "name", "location"]]

// Decompress the first user
console.log(decompress(cUsers[0], templates)); // { id: 3, name: "Finn", age: 28 }

Nested Objects and Arrays

Objects are compressed recursively, so nested objects and arrays — no matter how deep — are compressed as well.

import { compress } from "@sdgindex/compress";

const templates = [];
const user = {
  id: 1,
  name: "Finn",
  // A nested object
  address: {
    city: "Berlin",
    country: "Germany",
  },
};

console.log(compress(user, templates)); // [0, 1, "Finn", [1, "Berlin", "Germany"]]

console.log(templates); // [["id", "name", "address"], ["city", "country"]]

Background

When you have an array of objects that share the same or similar structure, the repeating object keys take up a sizeable amount of space.

For example, the users collection below:

// Lots of space taken up by repeating keys
const users = [
  {
    id: 1,
    name: "John",
    email: "[email protected]"
  }, {
    id: 2,
    name: "Mary",
    email: "[email protected]"
  },
  }, {
    id: 3,
    name: "Anna",
    country: "France",
  },
  ...
]

We can compress this collection of objects by storing only the values of each object:

const users = [
  [1, "John", "[email protected]"],
  [2, "Mary", "[email protected]"],
  [3, "Anna", "France"],
];

The object keys will be stored as templates, so that objects with the same structure can share the same template:

// We create one template for each unique object structure
const templates = [
  ["id", "name", "email"],
  ["id", "name", "country"],
];

// The first element in each user array references the template index
const users = [
  [0, 1, "John", "[email protected]"],
  [0, 2, "Mary", "[email protected]"],
  [1, 3, "Anna", "France"],
];

This compression "algorithm" is inspired by Steve Hanov's CJSON.