Skip to content

Commit

Permalink
perf: use an array to avoid string concats
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jul 26, 2023
1 parent d61d9bc commit 5b0e267
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/plainjs/dist/deno/utils/bufferedString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ export interface StringBuilder {

export class NonBufferedString implements StringBuilder {
private decoder = new TextDecoder("utf-8");
private string = "";
private strings: Array<string> = [];
public byteLength = 0;

public appendChar(char: number): void {
this.string += String.fromCharCode(char);
this.strings.push(String.fromCharCode(char));
this.byteLength += 1;
}

public appendBuf(buf: Uint8Array, start = 0, end: number = buf.length): void {
this.string += this.decoder.decode(buf.subarray(start, end));
this.strings.push(this.decoder.decode(buf.subarray(start, end)));
this.byteLength += end - start;
}

public reset(): void {
this.string = "";
this.strings = [];
this.byteLength = 0;
}

public toString(): string {
return this.string;
return this.strings.join('');
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/plainjs/src/utils/bufferedString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ export interface StringBuilder {

export class NonBufferedString implements StringBuilder {
private decoder = new TextDecoder("utf-8");
private string = "";
private strings: Array<string> = [];
public byteLength = 0;

public appendChar(char: number): void {
this.string += String.fromCharCode(char);
this.strings.push(String.fromCharCode(char));
this.byteLength += 1;
}

public appendBuf(buf: Uint8Array, start = 0, end: number = buf.length): void {
this.string += this.decoder.decode(buf.subarray(start, end));
this.strings.push(this.decoder.decode(buf.subarray(start, end)));
this.byteLength += end - start;
}

public reset(): void {
this.string = "";
this.strings = [];
this.byteLength = 0;
}

public toString(): string {
return this.string;
return this.strings.join("");
}
}

Expand Down

0 comments on commit 5b0e267

Please sign in to comment.