Skip to content

Instantly share code, notes, and snippets.

@dmarcelino
Created May 27, 2015 10:25
Show Gist options
  • Save dmarcelino/879d4da2a0e0c32f7d74 to your computer and use it in GitHub Desktop.
Save dmarcelino/879d4da2a0e0c32f7d74 to your computer and use it in GitHub Desktop.
Node Base62 libraries: b62 vs. base-x
var baseX = require('base-x')('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
var b62 = require('./b62');
var buf = new Buffer([0x30, 0x17, 0xe8, 0x92, 0xe2, 0x3d, 0xff]);
var str = "Hello World!";
var encodedX, encoded62, decodedX, decoded62, runs=100;
// warm up
console.time('base-x');
for(var i = 0; i < runs; i++){
encodedX = baseX.encode(buf, 'hex');
decodedX = baseX.decode(encodedX);
//console.log('x:', encodedX);
//console.log('x:', new Buffer(decodedX).toString('hex'));
encodedX = baseX.encode(new Buffer(str));
decodedX = baseX.decode(encodedX);
//console.log('x:', encodedX);
//console.log('x:', new Buffer(decodedX).toString());
}
console.timeEnd('base-x');
console.time('b62');
for(var i = 0; i < runs; i++){
encoded62 = b62.encode(buf);
decoded62 = b62.decode(encoded62, 'hex');
//console.log('b62:', encoded62);
//console.log('b62:', decoded62);
encoded62 = b62.encode(str);
decoded62 = b62.decode(encoded62);
//console.log('b62:', encoded62);
//console.log('x:', 'b62:', decoded62);
}
console.timeEnd('b62');
// Test
console.time('base-x');
for(var i = 0; i < runs; i++){
encodedX = baseX.encode(buf, 'hex');
decodedX = baseX.decode(encodedX);
encodedX = baseX.encode(new Buffer(str));
decodedX = baseX.decode(encodedX);
}
console.timeEnd('base-x');
console.time('b62');
for(var i = 0; i < runs; i++){
encoded62 = b62.encode(buf);
decoded62 = b62.decode(encoded62, 'hex');
encoded62 = b62.encode(str);
decoded62 = b62.decode(encoded62);
}
console.timeEnd('b62');
/**
* Results:
* base-x: 4ms
* b62: 113ms
* base-x: 4ms
* b62: 83ms
*
* base-x is clearly faster
* /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment