Skip to content

Commit

Permalink
strict equals (linting + minor speed enhancement)
Browse files Browse the repository at this point in the history
strict equals checking slightly improves performance in many javascript engines http://www.adequatelygood.com/Performance-of-vs-.html and it also prevents linters from throwing errors
  • Loading branch information
AJ ONeal committed Oct 22, 2015
1 parent 3a97530 commit 9179df9
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/thirty-two/thirty-two.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2011, Chris Umbel
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -17,7 +17,7 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
*/

var charTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
Expand All @@ -36,7 +36,7 @@ var byteTable = [

function quintetCount(buff) {
var quintets = Math.floor(buff.length / 5);
return buff.length % 5 == 0 ? quintets: quintets + 1;
return buff.length % 5 === 0 ? quintets: quintets + 1;
}

exports.encode = function(plain) {
Expand All @@ -53,7 +53,7 @@ exports.encode = function(plain) {
faster. will have to revisit. */
while(i < plain.length) {
var current = plain[i];

if(shiftIndex > 3) {
digit = current & (0xff >> shiftIndex);
shiftIndex = (shiftIndex + 5) % 8;
Expand All @@ -62,17 +62,17 @@ exports.encode = function(plain) {
i++;
} else {
digit = (current >> (8 - (shiftIndex + 5))) & 0x1f;
shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex == 0) i++;
shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex === 0) i++;
}

encoded[j] = charTable.charCodeAt(digit);
j++;
}

for(i = j; i < encoded.length; i++)
encoded[i] = 0x3d; //'='.charCodeAt(0)

return encoded;
};

Expand All @@ -85,23 +85,23 @@ exports.decode = function(encoded) {
encoded = new Buffer(encoded);
}
var decoded = new Buffer(Math.ceil(encoded.length * 5 / 8));

/* byte by byte isn't as pretty as octet by octet but tests a bit
faster. will have to revisit. */
faster. will have to revisit. */
for(var i = 0; i < encoded.length; i++) {
if(encoded[i] == 0x3d){ //'='
if(encoded[i] === 0x3d){ //'='
break;
}

var encodedByte = encoded[i] - 0x30;

if(encodedByte < byteTable.length) {
plainDigit = byteTable[encodedByte];

if(shiftIndex <= 3) {
shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex == 0) {

if(shiftIndex === 0) {
plainChar |= plainDigit;
decoded[plainPos] = plainChar;
plainPos++;
Expand Down

0 comments on commit 9179df9

Please sign in to comment.