Skip to content

Commit

Permalink
chore: stringify unit test and benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Jun 23, 2020
1 parent 1ec9f73 commit f2c57b9
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 30 deletions.
28 changes: 26 additions & 2 deletions examples/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,41 @@ const uuidv4 = (typeof window !== 'undefined' && window.uuidv4) || require('uuid
const uuidv3 = (typeof window !== 'undefined' && window.uuidv3) || require('uuid').v3;
const uuidv5 = (typeof window !== 'undefined' && window.uuidv5) || require('uuid').v5;
const uuidParse = (typeof window !== 'undefined' && window.uuidParse) || require('uuid').parse;
const uuidStringify =
(typeof window !== 'undefined' && window.uuidStringify) || require('uuid').stringify;

console.log('Starting. Tests take ~1 minute to run ...');

function testUuidToBytes() {
function testParse() {
const suite = new Benchmark.Suite({
onError(event) {
console.error(event.target.error);
},
});

const BYTES = [
0x0f,
0x5a,
0xbc,
0xd1,
0xc1,
0x94,
0x47,
0xf3,
0x90,
0x5b,
0x2d,
0xf7,
0x26,
0x3a,
0x08,
0x4b,
];

suite
.add('uuidStringify()', function () {
uuidStringify(BYTES);
})
.add('uuidParse()', function () {
uuidParse('0f5abcd1-c194-47f3-905b-2df7263a084b');
})
Expand Down Expand Up @@ -72,5 +96,5 @@ function testGeneration() {
.run();
}

testUuidToBytes();
testParse();
testGeneration();
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export { default as v5 } from './v5.js';
export { default as REGEX } from './regex.js';
export { default as version } from './version.js';
export { default as validate } from './validate.js';
export { default as stringify } from './stringify.js';
export { default as parse } from './parse.js';
58 changes: 30 additions & 28 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@ function parse(uuid) {
}

let v;
return Uint8Array.of(
// Parse ########-....-....-....-............
(v = parseInt(uuid.slice(0, 8), 16)) >>> 24,
(v >>> 16) & 0xff,
(v >>> 8) & 0xff,
v & 0xff,

// Parse ........-####-....-....-............
(v = parseInt(uuid.slice(9, 13), 16)) >>> 8,
v & 0xff,

// Parse ........-....-####-....-............
(v = parseInt(uuid.slice(14, 18), 16)) >>> 8,
v & 0xff,

// Parse ........-....-....-####-............
(v = parseInt(uuid.slice(19, 23), 16)) >>> 8,
v & 0xff,

// Parse ........-....-....-....-############
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
((v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000) & 0xff,
(v / 0x100000000) & 0xff,
(v >>> 24) & 0xff,
(v >>> 16) & 0xff,
(v >>> 8) & 0xff,
v & 0xff,
);
const arr = new Uint8Array(16);

// Parse ########-....-....-....-............
arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
arr[1] = (v >>> 16) & 0xff;
arr[2] = (v >>> 8) & 0xff;
arr[3] = v & 0xff;

// Parse ........-####-....-....-............
arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
arr[5] = v & 0xff;

// Parse ........-....-####-....-............
arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
arr[7] = v & 0xff;

// Parse ........-....-....-####-............
arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
arr[9] = v & 0xff;

// Parse ........-....-....-....-############
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
arr[10] = ((v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000) & 0xff;
arr[11] = (v / 0x100000000) & 0xff;
arr[12] = (v >>> 24) & 0xff;
arr[13] = (v >>> 16) & 0xff;
arr[14] = (v >>> 8) & 0xff;
arr[15] = v & 0xff;

return arr;
}

export default parse;
64 changes: 64 additions & 0 deletions test/unit/stringify.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import assert from 'assert';
import stringify from '../../src/stringify.js';

const BYTES = [
0x0f,
0x5a,
0xbc,
0xd1,
0xc1,
0x94,
0x47,
0xf3,
0x90,
0x5b,
0x2d,
0xf7,
0x26,
0x3a,
0x08,
0x4b,
];

describe('stringify', () => {
test('Stringify Array', () => {
assert.deepStrictEqual(stringify(BYTES), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});

test('Stringify TypedArray', () => {
assert.deepStrictEqual(
stringify(Uint8Array.from(BYTES)),
'0f5abcd1-c194-47f3-905b-2df7263a084b',
);
assert.deepStrictEqual(
stringify(Int32Array.from(BYTES)),
'0f5abcd1-c194-47f3-905b-2df7263a084b',
);
});

test('Stringify w/ offset', () => {
assert.deepStrictEqual(
stringify([0, 0, 0, ...BYTES], 3),
'0f5abcd1-c194-47f3-905b-2df7263a084b',
);
});

test('Throws on not enough values', () => {
const bytes = [...BYTES];
bytes.length = 15;
assert.throws(() => stringify(bytes));
});

test('Throws on undefined value', () => {
const bytes = [...BYTES];
delete bytes[3];
bytes.length = 15;
assert.throws(() => stringify(bytes));
});

test('Throws on invalid value', () => {
const bytes = [...BYTES];
bytes[3] = 256;
assert.throws(() => stringify(bytes));
});
});

0 comments on commit f2c57b9

Please sign in to comment.