-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize hexWrite with an array lookup table (#343)
Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
118 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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) | ||
|
||
const charset = '0123456789abcdef' | ||
|
||
let str = '' | ||
|
||
for (let i = 0; i < LENGTH * 2; i++) | ||
str += charset[Math.random() * charset.length | 0] | ||
|
||
suite | ||
.add('BrowserBuffer#write(' + LENGTH + ', "hex")', function () { | ||
browserSubject.write(str, 'hex') | ||
}) | ||
|
||
if (!process.browser) suite | ||
.add('NodeBuffer#write(' + LENGTH + ', "hex")', function () { | ||
nodeSubject.write(str, 'hex') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
|
||
const Buffer = require('../').Buffer | ||
const test = require('tape') | ||
|
||
test('buffer.write("hex") should stop on invalid characters', function (t) { | ||
// Test the entire 16-bit space. | ||
for (let ch = 0; ch <= 0xffff; ch++) { | ||
// 0-9 | ||
if (ch >= 0x30 && ch <= 0x39) { | ||
continue | ||
} | ||
|
||
// A-F | ||
if (ch >= 0x41 && ch <= 0x46) { | ||
continue | ||
} | ||
|
||
// a-f | ||
if (ch >= 0x61 && ch <= 0x66) { | ||
continue | ||
} | ||
|
||
for (const str of [ | ||
'abcd' + String.fromCharCode(ch) + 'ef0', | ||
'abcde' + String.fromCharCode(ch) + 'f0', | ||
'abcd' + String.fromCharCode(ch + 0) + String.fromCharCode(ch + 1) + 'f0', | ||
'abcde' + String.fromCharCode(ch + 0) + String.fromCharCode(ch + 1) + '0' | ||
]) { | ||
const buf = Buffer.alloc(4) | ||
t.equal(str.length, 8) | ||
t.equal(buf.write(str, 'hex'), 2) | ||
t.equal(buf.toString('hex'), 'abcd0000') | ||
t.equal(Buffer.from(str, 'hex').toString('hex'), 'abcd') | ||
} | ||
} | ||
|
||
t.end() | ||
}) | ||
|
||
test('buffer.write("hex") should truncate odd string lengths', function (t) { | ||
const buf = Buffer.alloc(32) | ||
const charset = '0123456789abcdef' | ||
|
||
let str = '' | ||
|
||
for (let i = 0; i < 63; i++) { | ||
str += charset[Math.random() * charset.length | 0] | ||
} | ||
|
||
t.equal(buf.write('abcde', 'hex'), 2) | ||
t.equal(buf.toString('hex', 0, 3), 'abcd00') | ||
|
||
buf.fill(0) | ||
|
||
t.equal(buf.write(str, 'hex'), 31) | ||
t.equal(buf.toString('hex', 0, 32), str.slice(0, -1) + '00') | ||
t.end() | ||
}) |