mirror of
https://github.com/codedread/bitjs
synced 2025-10-06 10:49:55 +02:00
Use JS Map instead of JS object for the huffman code tables. Seems to be a pretty good performance improvement on Chrome, Safari, Firefox.
This commit is contained in:
parent
4496948aaf
commit
843caf4e61
3 changed files with 63 additions and 9 deletions
|
@ -251,17 +251,14 @@ function getHuffmanCodes(bitLengths) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3: Assign numerical values to all codes
|
// Step 3: Assign numerical values to all codes
|
||||||
const table = {};
|
const table = new Map();
|
||||||
let tableLength = 0;
|
|
||||||
for (let n = 0; n < numLengths; ++n) {
|
for (let n = 0; n < numLengths; ++n) {
|
||||||
const len = bitLengths[n];
|
const len = bitLengths[n];
|
||||||
if (len != 0) {
|
if (len != 0) {
|
||||||
table[next_code[len]] = { length: len, symbol: n }; //, bitstring: binaryValueToString(next_code[len],len) };
|
table.set(next_code[len], { length: len, symbol: n }); //, bitstring: binaryValueToString(next_code[len],len) };
|
||||||
tableLength++;
|
|
||||||
next_code[len]++;
|
next_code[len]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
table.maxLength = tableLength;
|
|
||||||
|
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
@ -317,7 +314,6 @@ function getFixedDistanceTable() {
|
||||||
function decodeSymbol(bstream, hcTable) {
|
function decodeSymbol(bstream, hcTable) {
|
||||||
let code = 0;
|
let code = 0;
|
||||||
let len = 0;
|
let len = 0;
|
||||||
let match = false;
|
|
||||||
|
|
||||||
// loop until we match
|
// loop until we match
|
||||||
for (; ;) {
|
for (; ;) {
|
||||||
|
@ -327,16 +323,16 @@ function decodeSymbol(bstream, hcTable) {
|
||||||
++len;
|
++len;
|
||||||
|
|
||||||
// check against Huffman Code table and break if found
|
// check against Huffman Code table and break if found
|
||||||
if (hcTable.hasOwnProperty(code) && hcTable[code].length == len) {
|
if (hcTable.has(code) && hcTable.get(code).length == len) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (len > hcTable.maxLength) {
|
if (len > hcTable.maxLength) {
|
||||||
err(`Bit stream out of sync, didn't find a Huffman Code, length was ${len} ` +
|
err(`Bit stream out of sync, didn't find a Huffman Code, length was ${len} ` +
|
||||||
`and table only max code length of ${hcTable.maxLength}`);
|
`and table only max code length of ${hcTable.length}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hcTable[code].symbol;
|
return hcTable.get(code).symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -710,6 +706,7 @@ onmessage = function (event) {
|
||||||
bytestream = new bitjs.io.ByteStream(bytes);
|
bytestream = new bitjs.io.ByteStream(bytes);
|
||||||
} else {
|
} else {
|
||||||
bytestream.push(bytes);
|
bytestream.push(bytes);
|
||||||
|
// TODO: Shouldn't this return here if it's not waiting?
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unarchiveState === UnarchiveState.NOT_STARTED) {
|
if (unarchiveState === UnarchiveState.NOT_STARTED) {
|
||||||
|
|
13
tests/zipper-test.html
Normal file
13
tests/zipper-test.html
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="zipper-test.js" type="module"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<input id="zip-tester" type="file" multiple>
|
||||||
|
<span id="archive-uploader-label">Select a bunch of zip files</span>
|
||||||
|
</div>
|
||||||
|
<div id="result"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
44
tests/zipper-test.js
Normal file
44
tests/zipper-test.js
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
|
||||||
|
import { UnarchiveEventType, Unzipper } from '../archive/archive.js';
|
||||||
|
|
||||||
|
const result = document.querySelector('#result');
|
||||||
|
const fileInputEl = document.querySelector('#zip-tester');
|
||||||
|
|
||||||
|
async function getFiles(fileChangeEvt) {
|
||||||
|
result.innerHTML = `Starting to load files`;
|
||||||
|
const files = fileChangeEvt.target.files;
|
||||||
|
const buffers = [];
|
||||||
|
for (const file of files) {
|
||||||
|
buffers.push(await new Promise((resolve, reject) => {
|
||||||
|
const fr = new FileReader();
|
||||||
|
fr.onload = () => {
|
||||||
|
resolve(new Uint8Array(fr.result));
|
||||||
|
};
|
||||||
|
fr.readAsArrayBuffer(file);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
result.innerHTML = `Loaded files`;
|
||||||
|
|
||||||
|
let fileNum = 0;
|
||||||
|
const INC = 100 / files.length;
|
||||||
|
const start = performance.now();
|
||||||
|
|
||||||
|
for (const b of buffers) {
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
const unzipper = new Unzipper(b.buffer, { pathToBitJS: '../' });
|
||||||
|
unzipper.addEventListener(UnarchiveEventType.FINISH, () => {
|
||||||
|
fileNum++;
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
result.innerHTML = `Unzipping file ${fileNum} / ${files.length}`;
|
||||||
|
unzipper.start();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const end = performance.now();
|
||||||
|
result.innerHTML = `Unzipping took ${end - start}ms`;
|
||||||
|
}
|
||||||
|
|
||||||
|
fileInputEl.addEventListener('change', getFiles, false);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue