Giter VIP home page Giter VIP logo

varints's Introduction

简介

基于google protobuf 编码方式实现的类库,对于处理数字大小突变但是总体均衡的arraybuffer,varint对无符号整数进行操作,将较小的数字编码为更小的字节,实现压缩,最佳压缩率达到75%。

原理

将这个数组[1, 2, 257]转换成buffer:(三种方法

  1. 直接转成unit16array存储在buffer中占用6个字节。
  2. 更节省的方式是将[1, 2]转换成unit8array,再将257转换成unit16array,再将连个buffer连接起来,占用4字节。
  3. 使用varint解释这个3个数,对于0 ~ 2 ** 7的数占用一个字节,对于2 ** 7 ~ 2 ** 14 占用2个字节,总共占用4字节。

方法2,3占用字节相同但是对于解码的时候完全不同,方法2需要前后端沟通好offset为多少时将buffer使用另外一种视图读出,当某个offset的buffer 即可能是1字节,又可能是多字节时,这种方式无法处理。

方法3,将任意大小的数当作varint,压缩数据存储空间的同时,也解决解析得到问题。

使用

// 测试
npm run test

// 安装
npm i varInts --save

例子

const varInts = require('varInts');

const num = [ 1, 2, 257 ];
/*
* @param num {number | string | array | typeArray | arrayBuffer}
*/
const varInt = varInts.deflate(num); 
// => [2, 4, 130, 4]

const ab = new ArrayBuffer(varInt.length);
const view = new Uint8Array(ab);
for (let i = 0, len = varInt.length; i < len; i ++) {
    view[i] = varInt[i];
}
varInts.inflate(ab);
// => [1, 2, 257]
/*
* @param ab {ArrayBuffer}: 解码buffer 
* @param staroffset {number}: 解码起点
* @param num {number}: 从解析起点,解析num个varint
*/
//varInts(ab: ArrayBuffer, starOffset = 0, num = ab.byteLength)

varints's People

Contributors

takeern avatar

Watchers

 avatar  avatar

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.