ground:minimax

v1.1.3Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

ground:minimax Build Status

This package adds object compression and decompression of objects.

This is a small package that adds:

  • MiniMax.minify Compress object to array structure
  • MiniMax.maxify Decompress to object
  • MiniMax.stringify ~ Like a compressed version of EJSON.stringify
  • MiniMax.parse ~ Like the decomressing version of EJSON.parse

Usage

In short: Schema and schema less documents are minified to an array format:

  1. Array of keywords
  2. Array of data schemas
  3. Array of data
1  // The default got false, true, null and undefined in dictionary
2  var normalMiniMax = new MiniMax();
3
4  // Add additional words to the dictionary, eg. if used on a database,
5  // This will help MiniMax compress the data even more.
6  // The order and combination is vital for when uncompressing the data
7  var myMiniDB = new MiniMax({
8    dictionary: ['createdAt', 'createdBy', 'UpdatedAt', 'UpdatedBy'],
9    // progressive: false // Default true, puts values in dictionary
10    // - if false only keys are added
11  });
12
13  // By default an instance of MiniMax is available using the default
14  // dictionary
15  MiniMax.minify(obj, [skipFunctions=false])
16  MiniMax.maxify(obj)
17  MiniMax.stringify(obj)
18  MiniMax.parse(string)

How does it work? (example)

Our data object:

1var data = {
2  "5qSjMxCjkNF2SFBy6": {
3  _id: "5qSjMxCjkNF2SFBy6",
4  foo: "test"
5},
6  "rbieX9SbdGgfSWCd7": {
7  _id: "rbieX9SbdGgfSWCd7",
8  foo: "test",
9  bar: "okay"
10}
11};

In EJSON.stringify = 136 chars

1{"5qSjMxCjkNF2SFBy6":{"_id":"5qSjMxCjkNF2SFBy6","foo":"test"},
2"rbieX9SbdGgfSWCd7":{"_id":"rbieX9SbdGgfSWCd7","foo":"test",
3"bar":"okay"}} 

In EJSON.minify = 117 chars saved 14% space

1[["5qSjMxCjkNF2SFBy6","_id","foo","rbieX9SbdGgfSWCd7","bar"], // Keywords
2[0,[-1,2,4],[0,3]], // Schema
3[2,[1,0,"test"],[1,3,"test","okay"]]] // Data

The data array it self in this example is only about 36 chars ~ 27% of the original EJSON.stringify - if both server and client have keywords and the schema only the data would have to be sent.

1[2,[1,0,"test"],[1,3,"test","okay"]] 
1  Keywords:
2  [["5qSjMxCjkNF2SFBy6","_id","foo","rbieX9SbdGgfSWCd7","bar"],
3  Schemas:
4  [0,[-1,2,4],[0,3]],
5  data:
6  [2,[1,0,"test"],[1,3,"test","okay"]]] 

The keyword array contains key names

1  [1,0,"test"]
2  The "1" referes to the object schema [-1,2,4] - if pointing to
3  schema 0 then the data is an array.
4  {0, "test"}
5  In the schema the "-1", the minus says that the value is a
6  keyword reference and the 1 points to the keyword "_id"
7  In the schema the "2" points to keyword "foo" and use the value
8  in the data.
9  The "4" in the schema isnt used - this is an extension of the
10  schema to match the "bar" in the other object.
11  [-1,2,4] -> [_id, foo, bar_]
12  [-1,+2,+4] -> [0 = "5qSjMxCjkNF2SFBy6", "test"]
13  -> {_id: "5qSjMxCjkNF2SFBy6", foo: "test"_}
14

Future

  • Faster code
  • Better compression