<commit_before>#! /usr/bin/env node

var AsciiPack = require('./asciipack').AsciiPack;

function json_stringpack(obj){
  var lens = {}, ap, json;

  bench("AsciiPack.pack", function(){
    ap = AsciiPack.pack(obj);
  });
  bench("AsciiPack.unpack", function(){
    AsciiPack.unpack(ap);
  });
  lens.asciipack_length = ap.length;

  bench("JSON.stringify", function(){
    json = JSON.stringify(obj);
  });
  bench("JSON.parse", function(){
    JSON.parse(json);
  });
  lens.json_length = json.length;
  console.log(lens);
};

function bench(name, fn){
  var t = new Date();
  for(var i = 0; i < 100; i++) {
    fn();
  }
  console.log(name + ': ' + (new Date - t) + 'ms');
};

var hash = {};
var str16 = (new Array(0x1000)).join('a');
for (var i = 0; i < 50; i++) {
  var rand = i % 8;
  hash[i] = {
    0: [0,1,2,3,4,5,6,7,8,9],
    1: Math.random() + (Math.random() - 0.5) * 100000000,
    2: {hash:JSON.parse(JSON.stringify(hash))},
    3: '123abc',
    4: str16,
    5: null,
    6: false,
    7: true,
  }[rand];
}

json_stringpack(hash);<commit_msg>to detail<commit_after>#! /usr/bin/env node

var AsciiPack = require('./asciipack').AsciiPack;

function json_stringpack(name, obj){
  var lens = {}, ap, json;

  console.log('[' + name + ']');

  bench("AsciiPack.pack", function(){
    ap = AsciiPack.pack(obj);
  });
  bench("AsciiPack.unpack", function(){
    AsciiPack.unpack(ap);
  });

  bench("JSON.stringify", function(){
    json = JSON.stringify(obj);
  });
  bench("JSON.parse", function(){
    JSON.parse(json);
  });
  console.log({
    ap: ap,
    json: json
  });
};

function bench(name, fn){
  var t = new Date();
  for(var i = 0; i < 100000; i++) {
    fn();
  }
  console.log(name + ': ' + (new Date - t) + 'ms');
};

var str16 = (new Array(0x1000)).join('a');
var cases = {
  "positive fixint": 0,
  "uint 4": 16,
  "fixbin": "",
  "bin 8": '0123456789abcdef',
  "float 64": 1/3,
  "map 4": {},
  "array 4": [],
  "nil": null,
};

for (var key in cases) {
  json_stringpack(key, cases[key]);
}
{'fraction_non_alphanumeric': 0.2202970297029703, 'fraction_numerical': 0.041584158415841586, 'mean_word_length': 3.228033472803347, 'pattern_counts': {'":': 8, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1587124', 'n_tokens_mistral': 861, 'n_tokens_neox': 795, 'n_words': 174}