js-crc32/perf/utf8.js
SheetJS 1045f4f8e8 version bump 1.0.0: rolling checksums
- browser tests work in IE6+
- miscellaneous adjustments to tooling
2016-10-08 15:26:03 -04:00

54 lines
1.7 KiB
JavaScript

var T = require('../').table;
var old = require('crc-32').str;
var cur = require('../').str;
function sheetjs1(utf8) {
var buf = new Buffer(utf8);
for(var C = -1, i = 0; i != buf.length; ++i) {
C = (C >>> 8) ^ T[(C ^ buf[i]) & 0xFF];
}
return C ^ -1;
}
function sheetjs2(str) {
var C = -1;
for(var i = 0, L=str.length, c, d; i < L;) {
c = str.charCodeAt(i++);
if(c < 0x80) {
C = (C>>>8) ^ T[(C ^ c)&0xFF];
} else if(c < 0x800) {
C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
} else if(c >= 0xD800 && c < 0xE000) {
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
} else {
C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
}
}
return C ^ -1;
}
var foobar = "foo bar baz٪☃🍣";
for(var i = 0; i != 4; ++i) foobar += " " + foobar;
var assert = require('assert');
{
assert.equal(sheetjs1(foobar), sheetjs2(foobar));
assert.equal(sheetjs1(foobar), old(foobar));
assert.equal(sheetjs1(foobar), cur(foobar));
}
var BM = require('./bm');
var suite = new BM('unicode string');
suite.add('sheetjs 1', function() { for(var j = 0; j != 1000; ++j) sheetjs1(foobar); });
suite.add('sheetjs 2', function() { for(var j = 0; j != 1000; ++j) sheetjs2(foobar); });
suite.add('last vers', function() { for(var j = 0; j != 1000; ++j) old(foobar); });
suite.add('current v', function() { for(var j = 0; j != 1000; ++j) cur(foobar); });
suite.run();