Skip to content

Commit

Permalink
perf: Optimize ArrayType<T> Read & Write (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
MierenManz authored Jan 10, 2024
1 parent 796f19a commit c6906d7
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class ArrayType<T> extends UnsizedType<T[]> {

readPacked(dt: DataView, options: Options = { byteOffset: 0 }): T[] {
if (this.length === 0) return [];
const result = [];
result.length = this.length;
const result = new Array(this.length);
const { type } = this;

for (let i = 0; i < this.length; i++) {
result[i] = this.type.readPacked(dt, options);
for (let i = 0; i < result.length; i++) {
result[i] = type.readPacked(dt, options);
// No need for the increment offset. This is handled by the `type.readPacked` function
}

Expand All @@ -20,15 +20,15 @@ export class ArrayType<T> extends UnsizedType<T[]> {

read(dt: DataView, options: Options = { byteOffset: 0 }): T[] {
if (this.length === 0) return [];
const result: unknown[] = [];
result.length = this.length;
const result = new Array(this.length);
const { type } = this;

for (let i = 0; i < this.length; i++) {
result[i] = this.type.read(dt, options);
for (let i = 0; i < result.length; i++) {
result[i] = type.read(dt, options);
// No need for the increment offset. This is handled by the `type.read` function
}

return result as T[];
return result;
}

writePacked(
Expand All @@ -41,8 +41,9 @@ export class ArrayType<T> extends UnsizedType<T[]> {
}
if (value.length === 0) return;

for (let i = 0; i < this.length; i++) {
this.type.writePacked(value[i], dt, options);
const { type } = this;
for (let i = 0; i < value.length; i++) {
type.writePacked(value[i], dt, options);
// No need for the increment offset. This is handled by the `type.writePacked` function
}
}
Expand All @@ -57,8 +58,9 @@ export class ArrayType<T> extends UnsizedType<T[]> {
}
if (value.length === 0) return;

for (let i = 0; i < this.length; i++) {
this.type.write(value[i], dt, options);
const { type } = this;
for (let i = 0; i < value.length; i++) {
type.write(value[i], dt, options);
// No need for the increment offset. This is handled by the `type.write` function
}
}
Expand Down

0 comments on commit c6906d7

Please sign in to comment.