Browse Source

fromByteArray(): cache uint8.length value

master
Feross Aboukhadijeh 9 years ago
parent
commit
45e6df6d6b
  1. 9
      lib/b64.js

9
lib/b64.js

@ -82,7 +82,8 @@ function encodeChunk (uint8, start, end) {
function fromByteArray (uint8) {
var i
var extraBytes = uint8.length % 3 // if we have 1 byte left, pad 2 bytes
var len = uint8.length
var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
var output = ''
var parts = []
var temp, length
@ -90,20 +91,20 @@ function fromByteArray (uint8) {
// 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) {
for (i = 0, length = len - 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]
temp = uint8[len - 1]
output += lookup[temp >> 2]
output += lookup[(temp << 4) & 0x3F]
output += '=='
break
case 2:
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
temp = (uint8[len - 2] << 8) + (uint8[len - 1])
output += lookup[temp >> 10]
output += lookup[(temp >> 4) & 0x3F]
output += lookup[(temp << 2) & 0x3F]

Loading…
Cancel
Save