Skip to content

Instantly share code, notes, and snippets.

@SheetJSDev

SheetJSDev/bm.js Secret

Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SheetJSDev/a4b4a623a86b82fab08b to your computer and use it in GitHub Desktop.
Save SheetJSDev/a4b4a623a86b82fab08b to your computer and use it in GitHub Desktop.
/* bm.js (C) 2014 SheetJS -- http://sheetjs.com */
var Benchmark = require('benchmark');
var c = require('ansi')(process.stdout);
function test_end() { c.horizontalAbsolute(0).write("✓"); c.write('\n'); }
function suite_end() { console.log('Fastest is ' + this.filter('fastest').pluck('name')); }
function test_cycle(e) { c.horizontalAbsolute(0); c.eraseLine(); c.write("→ "+e.target); }
function BM(name) {
if(!(this instanceof BM)) return new BM(name);
console.log("--- " + name + " ---");
this.suite = new Benchmark.Suite(name, { onComplete: suite_end });
this.suites = [];
this.maxlen = 0;
}
BM.prototype.run = function() {
var maxlen = this.maxlen, ss = this.suite;
this.suites.forEach(function(s) { ss.add(s[0] + new Array(maxlen-s[0].length+1).join(" "), s[1]); });
if(this.suites.length > 0) this.suite.run();
};
BM.prototype.add = function(msg, test) {
this.suites.push([msg, {
onCycle: test_cycle,
onComplete: test_end,
defer: false,
fn: test
}]);
this.maxlen = Math.max(this.maxlen, msg.length);
};
module.exports = BM;
if(typeof require !== 'undefined') {
var js_crc32 = require('crc-32');
var pako_crc = require('./pako_crc32');
function b1(buf) { return js_crc32.buf(buf); }
function b2(buf) { return pako_crc(0, buf, buf.length, 0); }
function u1(str) { return js_crc32.str(str); }
function u2(str) { var o = Buffer(str); return pako_crc(0, o, o.length, 0); }
var ntests, len_max;
switch(process.env.MODE) {
case "A": ntests = 100000; len_max = 256; break;
case "B": ntests = 10000; len_max = 1024; break;
case "C": ntests = 10000; len_max = 4096; break;
case "D": ntests = 1000; len_max = 16384; break;
case "E": ntests = 1000; len_max = 65536; break;
case "F": ntests = 100; len_max = 262144; break;
case "G": ntests = 10; len_max = 1048576; break;
case "H": ntests = 10; len_max = 4194304; break;
case "I": ntests = 1; len_max = 16777216; break;
case "J": ntests = 1; len_max = 67108864; break;
case "K": ntests = 1; len_max = 268435456; break;
default: ntests = 10000; len_max = 1024; break;
}
var bstr_tests = [];
var ustr_tests = [];
var len_min = 1;
var corpus = new Array(0x0800);
for(var k = 0; k < 0x0800; ++k) corpus[k] = String.fromCharCode(k)
len_max --;
k = (Math.random()*0x800)|0;
for(var i = 0; i < ntests; ++i) {
var l = (Math.random() * (len_max - len_min))|0 + len_min;
var s = new Array(l), t = new Array(l);
for(var j = 0; j < l; ++j) s[j] = corpus[(i+j+k)&127];
for(var j = 0; j < l; ++j) t[j] = corpus[(i+j+k)&0x7FF];
bstr_tests[i] = [0, new Buffer(s.join(""))];
ustr_tests[i] = t.join("");
}
var assert = require('assert');
function fix(str) { return parseInt(str, 16)|0; }
for(var j = 0; j != ntests; ++j) {
assert.equal(b1(bstr_tests[j][1]), b2(bstr_tests[j][1]));
assert.equal(u1(ustr_tests[j]), u2(ustr_tests[j]));
}
var BM = require('./bm');
var suite = new BM('buffer (' + len_max + ')');
suite.add('js-crc32', function() { for(var j = 0; j != ntests; ++j) b1(bstr_tests[j][1]); });
suite.add('pako-crc32', function() { for(var j = 0; j != ntests; ++j) b2(bstr_tests[j][1]); });
suite.run();
var suite = new BM('unicode string (' + len_max + ')');
suite.add('js-crc32', function() { for(var j = 0; j != ntests; ++j) u1(ustr_tests[j]); });
suite.add('pako-crc32', function() { for(var j = 0; j != ntests; ++j) u2(ustr_tests[j]); });
suite.run();
}
--- buffer (255) ---
✓ js-crc32 x 30.60 ops/sec ±0.22% (54 runs sampled)
✓ pako-crc32 x 18.33 ops/sec ±0.42% (48 runs sampled)
Fastest is js-crc32
--- unicode string (255) ---
✓ js-crc32 x 11.55 ops/sec ±0.20% (32 runs sampled)
✓ pako-crc32 x 1.93 ops/sec ±3.39% (7 runs sampled)
Fastest is js-crc32
--- buffer (1023) ---
✓ js-crc32 x 78.57 ops/sec ±0.38% (81 runs sampled)
✓ pako-crc32 x 43.51 ops/sec ±0.35% (59 runs sampled)
Fastest is js-crc32
--- unicode string (1023) ---
✓ js-crc32 x 38.64 ops/sec ±0.34% (67 runs sampled)
✓ pako-crc32 x 6.42 ops/sec ±5.80% (18 runs sampled)
Fastest is js-crc32
--- buffer (4095) ---
✓ js-crc32 x 20.26 ops/sec ±0.32% (37 runs sampled)
✓ pako-crc32 x 11.19 ops/sec ±0.42% (32 runs sampled)
Fastest is js-crc32
--- unicode string (4095) ---
✓ js-crc32 x 8.19 ops/sec ±0.81% (26 runs sampled)
✓ pako-crc32 x 2.20 ops/sec ±6.21% (7 runs sampled)
Fastest is js-crc32
--- buffer (16383) ---
✓ js-crc32 x 50.55 ops/sec ±0.37% (66 runs sampled)
✓ pako-crc32 x 28.96 ops/sec ±0.68% (51 runs sampled)
Fastest is js-crc32
--- unicode string (16383) ---
✓ js-crc32 x 22.29 ops/sec ±0.66% (61 runs sampled)
✓ pako-crc32 x 4.47 ops/sec ±3.49% (13 runs sampled)
Fastest is js-crc32
--- buffer (65535) ---
✓ js-crc32 x 12.75 ops/sec ±0.40% (35 runs sampled)
✓ pako-crc32 x 7.21 ops/sec ±0.59% (21 runs sampled)
Fastest is js-crc32
--- unicode string (65535) ---
✓ js-crc32 x 8.55 ops/sec ±0.67% (29 runs sampled)
✓ pako-crc32 x 1.18 ops/sec ±1.98% (5 runs sampled)
Fastest is js-crc32
--- buffer (262143) ---
✓ js-crc32 x 33.24 ops/sec ±0.44% (58 runs sampled)
✓ pako-crc32 x 18.90 ops/sec ±0.73% (50 runs sampled)
Fastest is js-crc32
--- unicode string (262143) ---
✓ js-crc32 x 11.62 ops/sec ±0.77% (33 runs sampled)
✓ pako-crc32 x 2.52 ops/sec ±2.18% (7 runs sampled)
Fastest is js-crc32
--- buffer (1048575) ---
✓ js-crc32 x 97.70 ops/sec ±0.45% (83 runs sampled)
✓ pako-crc32 x 55.10 ops/sec ±0.54% (71 runs sampled)
Fastest is js-crc32
--- unicode string (1048575) ---
✓ js-crc32 x 34.49 ops/sec ±0.36% (60 runs sampled)
✓ pako-crc32 x 8.36 ops/sec ±1.64% (22 runs sampled)
Fastest is js-crc32
--- buffer (4194303) ---
✓ js-crc32 x 17.07 ops/sec ±0.61% (46 runs sampled)
✓ pako-crc32 x 9.63 ops/sec ±0.75% (27 runs sampled)
Fastest is js-crc32
--- unicode string (4194303) ---
✓ js-crc32 x 8.06 ops/sec ±0.80% (25 runs sampled)
✓ pako-crc32 x 1.97 ops/sec ±2.34% (7 runs sampled)
Fastest is js-crc32
--- buffer (16777215) ---
✓ js-crc32 x 112 ops/sec ±0.36% (82 runs sampled)
✓ pako-crc32 x 62.10 ops/sec ±0.31% (65 runs sampled)
Fastest is js-crc32
--- unicode string (16777215) ---
✓ js-crc32 x 38.33 ops/sec ±0.45% (67 runs sampled)
✓ pako-crc32 x 11.34 ops/sec ±2.86% (33 runs sampled)
Fastest is js-crc32
--- buffer (67108863) ---
✓ js-crc32 x 9.98 ops/sec ±0.38% (28 runs sampled)
✓ pako-crc32 x 5.60 ops/sec ±0.80% (17 runs sampled)
Fastest is js-crc32
--- unicode string (67108863) ---
✓ js-crc32 x 6.51 ops/sec ±0.61% (25 runs sampled)
✓ pako-crc32 x 1.74 ops/sec ±5.75% (8 runs sampled)
#!/bin/bash
cd misc &>/dev/null
echo "::: downloading modules"
npm install crc-32
curl https://raw.githubusercontent.com/nodeca/pako/master/lib/zlib/crc32.js > pako_crc32.js
for i in A B C D E F G H I J K; do MODE="$i" node integration.js; done
# for i in E F; do MODE="$i" node integration.js; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment