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

orle

v3.0.0

Published

RLE array encoder/decoder

Downloads

58

Readme

npm version CircleCI

orle

orle (pronounced "Oh, really?") is simple run length encoder for Javascript typed arrays.

Usage

Using orle is simple. There are two exposed methods: encode and decode.

Encoding

If you pass a typed array (such as Uint16Array or Float32Array) to encode, that data type is used. encode returns a promise with an encoded buffer. If you pass a non-typed array, it will try to find the most compact data type possible to encode the data. Note: if decimals are found, it will use Float64Array to preserve precision.

const orle = require('orle');
const buffer = await orle.encode([1,1,1,1,1,1,5,6,1,1,1,1,1,1,1,1,1]);

or

const orle = require('orle');
const buffer = await orle.encode(new Int8Array([1,1,1,1,1,1,5,6,1,1,1,1,1,1,1,1,1]));

If it's advantageous to gzip part of the payload, it will do so automatically. You can opt out of this by passing options to encode orle.encode(arr, { gzip: false }).

Notes

This package primarily optimizes typed arrays, which don't support null or undefined as elements. null or undefined get coerced to 0 when encoding.

Decoding

Pass a buffer to decode and you will get back a promise of a typed array.

const orle = require('orle');
const arr = await orle.decode(buffer);

Compression

Obviously your results may vary. If you have a large array with entirely non-repeating numbers, this will add about 9 bytes to the total payload. If you have a large array of entirely repeating numbers, the resulting payload will be about 10 bytes.

Format

The binary format is pretty simple:

Data Version

Bytes: 1 Sample Value: 7

Data Type/Lookup Table Flag

Bytes: 1 Sample Value: The first 7 bits are an unsigned int representing different data formats. Possible formats are:

  • 0: Int32
  • 1: Int16
  • 2: Int8
  • 3: Uint32
  • 4: Uint16
  • 5: Uint8
  • 6: Float32
  • 7: Float64
  • 8: String

If the last bit is set, that indicates that a lookup table is present If the second last bit is set, that indicates that the Payload is gzipped

Run Value Size

Bytes: 1 Sample Value: The size of each "run" value. Unsigned 8 bit int:

  • 0: Int32
  • 1: Int16
  • 2: Int8

Runs

Bytes: size-of-each-run-value * (number-of-distinct-runs+1) Sample Value: Store each set of run values. Positive values indicates that the value is repeated that number of times. Negative values indicates that there is a run of distinct values. 0 indicates there are no more runs defined

Lookup Table Length

Bytes: 0 or 1 Sample Value: If the lookup table bit was set, this indicates how many items are in the LUT (max of 256)

Lookup Table

Bytes: size-of-each-LUT-value * number-of-items-in-LUT Sample Value: The data is serialized flat and is obviously variable length. There are a maximum of 256 values in the lookup table. If a Lookup table is used, the payload items are serialized as Uint8s indicating the index into this array that they map to

GZip Payload

Bytes: A 4 byte UInt32 followed by a gzip payload Note: The Payload is gzipped if the second bit of the data type lookup is set. If the payload is gzipped, the rest of the storage format remains the same, except it is gzipped/gunzipped first.

Payload

Bytes: size-of-each-value * number-of-values-stored Sample Value: The actual payload. Important notes: The order here is very important and has to map to the runs previously defined. If a run is "positive" then the item should only appear once here.
Note on strings: Strings are stored as a Uint32LE number representing followed by that number of bytes of a JSON representation of the string array.
Note on Lookup Tables: If a lookup table is being used, the size of each value will be 1 and it will be a Uint8 value representing the value to use from the lookup table.