Browse Source

Merge pull request #21 from beatgammit/improve-perf

Make decode (toByteArray()) 3x faster! (Fix #20)
master
Feross Aboukhadijeh 9 years ago
parent
commit
a204752e3f
  1. 14
      bench/basic.js
  2. 30
      bench/bench.js
  3. 199
      lib/b64.js
  4. 1
      package.json

14
bench/basic.js

@ -0,0 +1,14 @@
var random = require('crypto').pseudoRandomBytes
var b64 = require('../')
var data = random(1e6).toString('base64')
var start = Date.now()
var raw = b64.toByteArray(data)
var middle = Date.now()
data = b64.fromByteArray(raw)
var end = Date.now()
console.log('decode ms, decode ops/ms, encode ms, encode ops/ms')
console.log(
middle - start, data.length / (middle - start),
end - middle, data.length / (end - middle))

30
bench/bench.js

@ -1,14 +1,22 @@
var random = require('crypto').pseudoRandomBytes
var base64 = require('../')
var benchmark = require('benchmark')
var b64 = require('../')
var suite = new benchmark.Suite()
var random = require('crypto').pseudoRandomBytes
var data = random(1e6).toString('base64')
var start = Date.now()
var raw = b64.toByteArray(data)
var middle = Date.now()
data = b64.fromByteArray(raw)
var end = Date.now()
var raw = base64.toByteArray(data)
console.log('decode ms, decode ops/ms, encode ms, encode ops/ms')
console.log(
middle - start, data.length / (middle - start),
end - middle, data.length / (end - middle))
suite
.add('base64.toByteArray() (decode)', function () {
var raw2 = base64.toByteArray(data) // eslint-disable-line no-unused-vars
})
.add('base64.fromByteArray() (encode)', function () {
var data2 = base64.fromByteArray(raw) // eslint-disable-line no-unused-vars
})
.on('error', function (event) {
console.error(event.target.error.stack)
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.run({ async: true })

199
lib/b64.js

@ -1,131 +1,114 @@
;(function (exports) {
'use strict'
'use strict'
exports.toByteArray = toByteArray
exports.fromByteArray = fromByteArray
var lookup = []
var revLookup = []
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
function init () {
var i
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
var lookup = []
for (i = 0; i < code.length; i++) {
var len = code.length
for (i = 0; i < len; i++) {
lookup[i] = code[i]
}
var revLookup = []
for (i = 0; i < code.length; ++i) {
for (i = 0; i < len; ++i) {
revLookup[code.charCodeAt(i)] = i
}
revLookup['-'.charCodeAt(0)] = 62
revLookup['_'.charCodeAt(0)] = 63
}
var Arr = (typeof Uint8Array !== 'undefined')
? Uint8Array
: Array
init()
function decode (elt) {
var v = revLookup[elt.charCodeAt(0)]
return v !== undefined ? v : -1
}
function toByteArray (b64) {
var i, j, l, tmp, placeHolders, arr
var len = b64.length
function b64ToByteArray (b64) {
var i, j, l, tmp, placeHolders, arr
if (b64.length % 4 > 0) {
throw new Error('Invalid string. Length must be a multiple of 4')
}
// the number of equal signs (place holders)
// if there are two placeholders, than the two characters before it
// represent one byte
// if there is only one, then the three characters before it represent 2 bytes
// this is just a cheap hack to not do indexOf twice
var len = b64.length
placeHolders = b64.charAt(len - 2) === '=' ? 2 : b64.charAt(len - 1) === '=' ? 1 : 0
// base64 is 4/3 + up to two characters of the original data
arr = new Arr(b64.length * 3 / 4 - placeHolders)
// if there are placeholders, only get up to the last complete 4 chars
l = placeHolders > 0 ? b64.length - 4 : b64.length
var L = 0
function push (v) {
arr[L++] = v
}
for (i = 0, j = 0; i < l; i += 4, j += 3) {
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
push((tmp & 0xFF0000) >> 16)
push((tmp & 0xFF00) >> 8)
push(tmp & 0xFF)
}
if (placeHolders === 2) {
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
push(tmp & 0xFF)
} else if (placeHolders === 1) {
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
push((tmp >> 8) & 0xFF)
push(tmp & 0xFF)
}
return arr
if (len % 4 > 0) {
throw new Error('Invalid string. Length must be a multiple of 4')
}
function encode (num) {
return lookup[num]
// the number of equal signs (place holders)
// if there are two placeholders, than the two characters before it
// represent one byte
// if there is only one, then the three characters before it represent 2 bytes
// this is just a cheap hack to not do indexOf twice
placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0
// base64 is 4/3 + up to two characters of the original data
arr = new Arr(len * 3 / 4 - placeHolders)
// if there are placeholders, only get up to the last complete 4 chars
l = placeHolders > 0 ? len - 4 : len
var L = 0
for (i = 0, j = 0; i < l; i += 4, j += 3) {
tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]
arr[L++] = (tmp & 0xFF0000) >> 16
arr[L++] = (tmp & 0xFF00) >> 8
arr[L++] = tmp & 0xFF
}
function tripletToBase64 (num) {
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)
if (placeHolders === 2) {
tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)
arr[L++] = tmp & 0xFF
} else if (placeHolders === 1) {
tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)
arr[L++] = (tmp >> 8) & 0xFF
arr[L++] = tmp & 0xFF
}
function encodeChunk (uint8, start, end) {
var temp
var output = []
for (var i = start; i < end; i += 3) {
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
output.push(tripletToBase64(temp))
}
return output.join('')
return arr
}
function tripletToBase64 (num) {
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
}
function encodeChunk (uint8, start, end) {
var tmp
var output = []
for (var i = start; i < end; i += 3) {
tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
output.push(tripletToBase64(tmp))
}
return output.join('')
}
function fromByteArray (uint8) {
var tmp
var len = uint8.length
var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
var output = ''
var parts = []
var maxChunkLength = 16383 // must be multiple of 3
// go through the array every three bytes, we'll deal with trailing stuff later
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
}
function uint8ToBase64 (uint8) {
var i
var extraBytes = uint8.length % 3 // if we have 1 byte left, pad 2 bytes
var output = ''
var parts = []
var temp, length
var maxChunkLength = 16383 // must be multiple of 3
// go through the array every three bytes, we'll deal with trailing stuff later
for (i = 0, length = uint8.length - extraBytes; i < length; i += maxChunkLength) {
parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > length ? length : (i + maxChunkLength)))
}
// pad the end with zeros, but make sure to not forget the extra bytes
switch (extraBytes) {
case 1:
temp = uint8[uint8.length - 1]
output += encode(temp >> 2)
output += encode((temp << 4) & 0x3F)
output += '=='
break
case 2:
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
output += encode(temp >> 10)
output += encode((temp >> 4) & 0x3F)
output += encode((temp << 2) & 0x3F)
output += '='
break
default:
break
}
parts.push(output)
return parts.join('')
// pad the end with zeros, but make sure to not forget the extra bytes
if (extraBytes === 1) {
tmp = uint8[len - 1]
output += lookup[tmp >> 2]
output += lookup[(tmp << 4) & 0x3F]
output += '=='
} else if (extraBytes === 2) {
tmp = (uint8[len - 2] << 8) + (uint8[len - 1])
output += lookup[tmp >> 10]
output += lookup[(tmp >> 4) & 0x3F]
output += lookup[(tmp << 2) & 0x3F]
output += '='
}
exports.toByteArray = b64ToByteArray
exports.fromByteArray = uint8ToBase64
}(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
parts.push(output)
return parts.join('')
}

1
package.json

@ -14,6 +14,7 @@
"license": "MIT",
"dependencies": {},
"devDependencies": {
"benchmark": "^2.1.0",
"standard": "^6.0.5",
"tape": "4.x"
}

Loading…
Cancel
Save