mirror of
https://github.com/0x5eal/rbxts-pako.git
synced 2025-04-10 22:00:58 +01:00
Optimized chunks flatten
This commit is contained in:
parent
8ea9fc4924
commit
c78fe44ca1
2 changed files with 33 additions and 19 deletions
|
@ -126,26 +126,8 @@ function deflate(input, options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
deflator.onEnd = function(error) {
|
deflator.onEnd = function(error) {
|
||||||
var i, l, len, pos, chunk;
|
|
||||||
|
|
||||||
if (error) { throw error; }
|
if (error) { throw error; }
|
||||||
|
result = utils.flattenChunks(chunks);
|
||||||
// calculate data length
|
|
||||||
len = 0;
|
|
||||||
for (i=0, l=chunks.length; i<l; i++) {
|
|
||||||
len += chunks[i].length;
|
|
||||||
}
|
|
||||||
|
|
||||||
// join chunks
|
|
||||||
result = utils.arrayCreate(len);
|
|
||||||
pos = 0;
|
|
||||||
|
|
||||||
for (i=0, l=chunks.length; i<l; i++) {
|
|
||||||
chunk = chunks[i];
|
|
||||||
len = chunk.length;
|
|
||||||
utils.arraySet(result, chunk, 0, len, pos);
|
|
||||||
pos += len;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
deflator.push(input);
|
deflator.push(input);
|
||||||
|
|
|
@ -84,6 +84,8 @@ exports.array16Create = function (length) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Fill Array || Typed Array with constant
|
||||||
|
//
|
||||||
exports.fill = function (buf, val) {
|
exports.fill = function (buf, val) {
|
||||||
var len = buf.length;
|
var len = buf.length;
|
||||||
|
|
||||||
|
@ -92,3 +94,33 @@ exports.fill = function (buf, val) {
|
||||||
// fastest for untyped Array
|
// fastest for untyped Array
|
||||||
while (--len) { buf[len] = val; }
|
while (--len) { buf[len] = val; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Join array of chunks to single array.
|
||||||
|
// Expect Array of (Array(Bytes) || Uint8Array).
|
||||||
|
//
|
||||||
|
exports.flattenChunks = function(chunks) {
|
||||||
|
var i, l, len, pos, chunk, result;
|
||||||
|
|
||||||
|
if (typedOk()) {
|
||||||
|
// calculate data length
|
||||||
|
len = 0;
|
||||||
|
for (i=0, l=chunks.length; i<l; i++) {
|
||||||
|
len += chunks[i].length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// join chunks
|
||||||
|
result = new Uint8Array(len);
|
||||||
|
pos = 0;
|
||||||
|
for (i=0, l=chunks.length; i<l; i++) {
|
||||||
|
chunk = chunks[i];
|
||||||
|
result.set(chunk, pos);
|
||||||
|
pos += chunk.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback for untyped arrays
|
||||||
|
return [].concat.apply([], chunks);
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue