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

c8r

v1.0.3

Published

Encode/decode array of numbers into optimized Base 64 URL compatible

Downloads

1

Readme

Intro

Compressor is a Javascript encoder/decoder for array of numbers. It encodes numbers into an efficient Base 64 URL string.

Input

[5, 0, 5, 6, 3, 4, 5, 0, 5, 6];

Output

"Coucou";

Compressor can encode any array of numbers, and decode Base 64 URL string.

Usage

Install with npm or yarn

npm install c8r

Or

yarn add c8r

Encode

encode([1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1]));

Returns "Ahh"

Decode

decode("ChaLut");

Returns [4, 1, 3, 2, 1, 3, 5, 6, 5, 5]

Use case

The original use case was to send an array of numbers directly in URL, like this: myapplication.io/?collection=[1, 2, 3, 4, 50, 600…]

But this is not an efficient way of doing so, because each digit will use a character (which is 8 or 16 bits), each comma separator will also use a character, and each bracket…

So we look for an alternative, like encoding into Base 64

Why not using btoa ?

btoa is used to transform Binary String into ASCII Base 64.

Example:

btoa([1, 2, 3, 4, 5, 6, 7, 8]);

Returns "MSwyLDMsNCw1LDYsNyw4"

atob("MSwyLDMsNCw1LDYsNyw4");

Returns "1,2,3,4,5,6,7,8"

This is very inefficient, you will use 16 bits per each digit (due to UTF-16 used for binary string), + 16 bits for each ',' delimiter…

You can try to optimize this, using for example Typed Array

var buffer = new Uint8Array(input);
var binary = "";
for (var b = 0; b < buffer.byteLength; b++) {
  binary += String.fromCharCode(buffer[b]);
}
return window.btoa(binary);

Example: input = [1,2,3,4,5,6] returns "AQIDBAUG"

This is better, but not optimal.

It got several flaws, beginning to size limitation in your input (8 bits per number, which won’t let you use numbers greater than 255), and increasing the total size as String.fromCharCode will encode into 16 bits…

You can also try to combine two 8 bits integers into one 16 bits char:

const [first, second] = [42, 12];

String.fromCharCode((first << 8) + second); // returns "⨌"

Unfortunately btoa doesn’t accept out of ASCII range characters…

How is this working ?

The first optimization is finding the smallest size for encoding each numbers gave to the encode function.

For example, taking the following input [1, 2, 3, 0, 2, 3, 1, 3, 2, 2, 3, 0, 0, 3, 1]

2 bits are needed to encode each number (as it could contain 0, 1, 2, or 3)

Giving that, the first Base 64 character of the output is this number of bits used for encoding each number.

Example: 2 bits => the output will begin with B character.

Next, each number is grouped into 6 bits packet for creating a Base 64 symbol.

Example: [1, 2, 3] => became 01 10 11 which is b in Base 64.

Resulting into BbLesN (more compact than [1, 2, 3, 0, 2, 3, 1, 3, 2, 2, 3, 0, 0, 3, 1] isn’t it?)