Giter Site home page Giter Site logo

base256-encoding's Introduction

Base256

Base256 encoding, a.k.a. latin1 encoding, the most memory-efficient encoding possible in JavaScript.

Features

If a string contains only the 256 codepoints from \u0000 to \u00ff then in modern engine it will consume one byte of memory per character. If that's not true then two (!) bytes of memory per character will be consumed, even if only one character goes out of range. Base64 falls within the golden range but it only uses 25% of the available codepoints, Base256 uses all of them and is therefore more memory efficient.

If you need the encoded string to be URL-friendly or something or if you are encoding tiny things you should go with Base64, otherwise using Base256 will save you a decent amount of memory.

Install

npm install --save base256-encoding

Usage

import Base256 from 'base256-encoding';

// Uint8Array encoding & decoding

{
  const raw = 'Hello ๐Ÿ˜ƒ';
  const uint8 = new TextEncoder ().encode ( raw );
  console.log ( uint8 ); // => Uint8Array(10) [ 72, 101, 108, 108, 111,  32, 240, 159, 152, 131 ]

  const encoded = Base256.encode ( uint8 );
  console.log ( encoded ); // => 'Hello รฐ\x9F\x98\x83'

  const decoded = Base256.decodeStr ( encoded );
  console.log ( decoded ); // => // => Uint8Array(10) [ 72, 101, 108, 108, 111,  32, 240, 159, 152, 131 ]
}

// String encoding & decoding

{
  const raw = 'Hello ๐Ÿ˜ƒ';
  const encoded = Base256.encodeStr ( raw );
  console.log ( encoded ); // => 'Hello รฐ\x9F\x98\x83'

  const decoded = Base256.decodeStr ( encoded );
  console.log ( decoded ); // => 'Hello ๐Ÿ˜ƒ'
}

// Check if a string is base256-encoded

{
  console.log ( Base256.is ( 'Hello' ) ); // => true
  console.log ( Base256.is ( '๐Ÿ˜ƒ' ) ); // => false
}

License

MIT ยฉ Fabio Spampinato

base256-encoding's People

Contributors

fabiospampinato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

base256-encoding's Issues

Use with file

For a little project I'm working I need to encode a zip file with base256. This works with base64 online however I am unsure how to read a file into your code? I am very new at all of this so any help would be greatly appreciated!

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.