Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance gains for hex and base64 #366

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.airtap.yml
*.log
node_modules/
perf/bundle.js
package-lock.json
9 changes: 8 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
- jkkang (jkkang@smartauth.kr)
- Deklan Webster (deklanw@gmail.com)
- Martin Heidegger (martin.heidegger@gmail.com)
- junderw (junderwood@bitcoinbank.co.jp)
- André Werlang (589286+awerlang@users.noreply.github.com)
- Bradley Odell (btodell@hotmail.com)
- Dominik Moritz (domoritz@gmail.com)
- Rachel Simone Weil (partytimehexcellent@gmail.com)
- Patrick McAndrew (urg@users.noreply.github.com)
- Jonathan Underwood (jonathan.underwood4649@gmail.com)
- Christopher Jeffrey (JJ) (chjjeffrey@gmail.com)
- George MacKerron (george@mackerron.co.uk)

#### Generated by bin/update-authors.sh.
86 changes: 12 additions & 74 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

'use strict'

const base64 = require('base64-js')
const hextreme = require('hextreme')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new dependency as non-battle-tested as a newly published package - unfortunately - isn't tenable for Buffer.

The TextDecoder option presented previously was an easy option as the complexity of review remained in band.

const ieee754 = require('ieee754')
const customInspectSymbol =
(typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
Expand Down Expand Up @@ -847,20 +847,10 @@ function hexWrite (buf, string, offset, length) {
length = strLen >>> 1
}

for (let i = 0; i < length; ++i) {
const a = string.charCodeAt(i * 2 + 0)
const b = string.charCodeAt(i * 2 + 1)
const hi = hexCharValueTable[a & 0x7f]
const lo = hexCharValueTable[b & 0x7f]

if ((a | b | hi | lo) & ~0x7f) {
return i
}

buf[offset + i] = (hi << 4) | lo
}

return length
if (string.length > length << 1) string = string.slice(0, length << 1)
const data = hextreme.fromHex(string, { onInvalidInput: 'truncate' })
buf.set(data, offset)
return data.length
}

function utf8Write (buf, string, offset, length) {
Expand Down Expand Up @@ -957,9 +947,9 @@ Buffer.prototype.toJSON = function toJSON () {

function base64Slice (buf, start, end) {
if (start === 0 && end === buf.length) {
return base64.fromByteArray(buf)
return hextreme.toBase64(buf)
} else {
return base64.fromByteArray(buf.slice(start, end))
return hextreme.toBase64(buf.slice(start, end))
}
}

Expand Down Expand Up @@ -1088,11 +1078,11 @@ function hexSlice (buf, start, end) {
if (!start || start < 0) start = 0
if (!end || end < 0 || end > len) end = len

let out = ''
for (let i = start; i < end; ++i) {
out += hexSliceLookupTable[buf[i]]
if (start === 0 && end === buf.length) {
return hextreme.toHex(buf)
} else {
return hextreme.toHex(buf.slice(start, end))
}
return out
}

function utf16leSlice (buf, start, end) {
Expand Down Expand Up @@ -1943,22 +1933,6 @@ function boundsError (value, length, type) {
// HELPER FUNCTIONS
// ================

const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g

function base64clean (str) {
// Node takes equal signs as end of the Base64 encoding
str = str.split('=')[0]
// Node strips out invalid characters like \n and \t from the string, base64-js does not
str = str.trim().replace(INVALID_BASE64_RE, '')
// Node converts strings with length < 2 to ''
if (str.length < 2) return ''
// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
while (str.length % 4 !== 0) {
str = str + '='
}
return str
}

function utf8ToBytes (string, units) {
units = units || Infinity
let codePoint
Expand Down Expand Up @@ -2065,7 +2039,7 @@ function utf16leToBytes (str, units) {
}

function base64ToBytes (str) {
return base64.toByteArray(base64clean(str))
return hextreme.fromBase64(str, { onInvalidInput: 'skip', alphabet: 'base64any' })
}

function blitBuffer (src, dst, offset, length) {
Expand All @@ -2091,42 +2065,6 @@ function numberIsNaN (obj) {
return obj !== obj // eslint-disable-line no-self-compare
}

// Create lookup table for `toString('hex')`
// See: https://github.com/feross/buffer/issues/219
const hexSliceLookupTable = (function () {
const alphabet = '0123456789abcdef'
const table = new Array(256)
for (let i = 0; i < 16; ++i) {
const i16 = i * 16
for (let j = 0; j < 16; ++j) {
table[i16 + j] = alphabet[i] + alphabet[j]
}
}
return table
})()

// hex lookup table for Buffer.from(x, 'hex')
/* eslint-disable no-multi-spaces, indent */
const hexCharValueTable = [
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1
]
/* eslint-enable no-multi-spaces, indent */

// Return not function with Error if BigInt not supported
function defineBigIntMethod (fn) {
return typeof BigInt === 'undefined' ? BufferBigIntNotDefined : fn
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"James Halliday <mail@substack.net>"
],
"dependencies": {
"base64-js": "^1.3.1",
"hextreme": "^1.0.5",
"ieee754": "^1.2.1"
},
"devDependencies": {
Expand Down Expand Up @@ -59,7 +59,7 @@
},
"scripts": {
"perf": "browserify --debug perf/bracket-notation.js > perf/bundle.js && open perf/index.html",
"perf-node": "node perf/bracket-notation.js && node perf/concat.js && node perf/copy-big.js && node perf/copy.js && node perf/new-big.js && node perf/new.js && node perf/readDoubleBE.js && node perf/readFloatBE.js && node perf/readUInt32LE.js && node perf/slice.js && node perf/writeFloatBE.js && node perf/write-hex.js",
"perf-node": "node perf/toString-hex.js && node perf/from-hex.js && node perf/toString-base64.js && node perf/from-base64.js && node perf/bracket-notation.js && node perf/concat.js && node perf/copy-big.js && node perf/copy.js && node perf/new-big.js && node perf/new.js && node perf/readDoubleBE.js && node perf/readFloatBE.js && node perf/readUInt32LE.js && node perf/slice.js && node perf/writeFloatBE.js && node perf/write-hex.js",
"size": "browserify -r ./ | uglifyjs -c -m | gzip | wc -c",
"standard": "standard",
"test": "tape test/*.js test/node/*.js",
Expand Down
22 changes: 22 additions & 0 deletions perf/from-base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const BrowserBuffer = require('../').Buffer // (this module)
const util = require('./util')
const suite = util.suite()

const LENGTH = 4096
const browserSubject = BrowserBuffer.alloc(LENGTH)

for (let i = 0; i < LENGTH; i++) {
browserSubject[i] = (Math.random() * 255) << 0
}

const base64 = browserSubject.toString('base64')

suite
.add('BrowserBuffer#from(base64String, "base64")', function () {
BrowserBuffer.from(base64, 'base64')
})

if (!process.browser) suite
.add('NodeBuffer#from(base64String, "base64")', function () {
Buffer.from(base64, 'base64')
})
22 changes: 22 additions & 0 deletions perf/from-hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const BrowserBuffer = require('../').Buffer // (this module)
const util = require('./util')
const suite = util.suite()

const LENGTH = 4096
const browserSubject = BrowserBuffer.alloc(LENGTH)

for (let i = 0; i < LENGTH; i++) {
browserSubject[i] = (Math.random() * 255) << 0
}

const hex = browserSubject.toString('hex')

suite
.add('BrowserBuffer#from(hexString, "hex")', function () {
BrowserBuffer.from(hex, 'hex')
})

if (!process.browser) suite
.add('NodeBuffer#from(hexString, "hex")', function () {
Buffer.from(hex, 'hex')
})
21 changes: 21 additions & 0 deletions perf/toString-base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const BrowserBuffer = require('../').Buffer // (this module)
const util = require('./util')
const suite = util.suite()

const LENGTH = 4096
const browserSubject = BrowserBuffer.alloc(LENGTH)
const nodeSubject = Buffer.alloc(LENGTH)

for (let i = 0; i < LENGTH; i++) {
browserSubject[i] = nodeSubject[i] = (Math.random() * 255) << 0
}

suite
.add('BrowserBuffer#toString("base64")', function () {
browserSubject.toString('base64')
})

if (!process.browser) suite
.add('NodeBuffer#toString("base64")', function () {
nodeSubject.toString('base64')
})
21 changes: 21 additions & 0 deletions perf/toString-hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const BrowserBuffer = require('../').Buffer // (this module)
const util = require('./util')
const suite = util.suite()

const LENGTH = 4096
const browserSubject = BrowserBuffer.alloc(LENGTH)
const nodeSubject = Buffer.alloc(LENGTH)

for (let i = 0; i < LENGTH; i++) {
browserSubject[i] = nodeSubject[i] = (Math.random() * 255) << 0
}

suite
.add('BrowserBuffer#toString("hex")', function () {
browserSubject.toString('hex')
})

if (!process.browser) suite
.add('NodeBuffer#toString("hex")', function () {
nodeSubject.toString('hex')
})