Skip to content

Commit 447adbf

Browse files
committed
Improve code style (cache array lengths, rename constant)
1 parent e06a751 commit 447adbf

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

KaitaiStream.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,9 @@ class KaitaiStream {
860860
const mask = groupSize * 8 - 1;
861861
const antiAmount = -amount & mask;
862862

863-
const r = new Uint8Array(data.length);
864-
for (let i = 0; i < data.length; i++)
863+
const dl = data.length;
864+
const r = new Uint8Array(dl);
865+
for (let i = 0; i < dl; i++)
865866
r[i] = (data[i] << amount) & 0xff | (data[i] >> antiAmount);
866867

867868
return r;
@@ -922,9 +923,9 @@ class KaitaiStream {
922923
*/
923924
public static arrayMin(arr: ArrayLike<number>): number {
924925
let min = arr[0];
925-
let x;
926-
for (let i = 1, n = arr.length; i < n; ++i) {
927-
x = arr[i];
926+
const n = arr.length;
927+
for (let i = 1; i < n; i++) {
928+
const x = arr[i];
928929
if (x < min) min = x;
929930
}
930931
return min;
@@ -938,9 +939,9 @@ class KaitaiStream {
938939
*/
939940
public static arrayMax(arr: ArrayLike<number>): number {
940941
let max = arr[0];
941-
let x;
942-
for (let i = 1, n = arr.length; i < n; ++i) {
943-
x = arr[i];
942+
const n = arr.length;
943+
for (let i = 1; i < n; i++) {
944+
const x = arr[i];
944945
if (x > max) max = x;
945946
}
946947
return max;
@@ -1016,10 +1017,11 @@ class KaitaiStream {
10161017
* @returns String created from the character codes.
10171018
*/
10181019
protected static createStringFromArray(array: Uint8Array): string {
1019-
const chunk_size = 0x8000;
1020+
const CHUNK_SIZE = 0x8000;
10201021
const chunks = [];
1021-
for (let i = 0; i < array.length; i += chunk_size) {
1022-
const chunk = array.subarray(i, i + chunk_size);
1022+
const len = array.length;
1023+
for (let i = 0; i < len; i += CHUNK_SIZE) {
1024+
const chunk = array.subarray(i, i + CHUNK_SIZE);
10231025
chunks.push(String.fromCharCode.apply(null, chunk));
10241026
}
10251027
return chunks.join("");

0 commit comments

Comments
 (0)