mirror of
https://github.com/fluencelabs/assemblyscript-json
synced 2025-03-15 03:00:50 +00:00
Fix O(N^2) string concatenation in JSON encoder
This commit is contained in:
parent
5855cde762
commit
e564e7171c
@ -3,13 +3,13 @@ declare function logF64(val: f64): void;
|
||||
|
||||
export class JSONEncoder {
|
||||
private isFirstKey: boolean = true
|
||||
private inObject: Array<boolean> = [false]
|
||||
private result: string = ""
|
||||
private result: string[] = new Array<string>();
|
||||
|
||||
serialize(): Uint8Array {
|
||||
// TODO: Write directly to UTF8 bytes
|
||||
let utf8ptr = this.result.toUTF8();
|
||||
let buffer = new Uint8Array(this.result.lengthUTF8);
|
||||
let result = this.toString();
|
||||
let utf8ptr = result.toUTF8();
|
||||
let buffer = new Uint8Array(result.lengthUTF8);
|
||||
for (let i = 0; i < buffer.length; i++) {
|
||||
buffer[i] = load<u8>(utf8ptr + i);
|
||||
}
|
||||
@ -17,7 +17,7 @@ export class JSONEncoder {
|
||||
}
|
||||
|
||||
toString(): String {
|
||||
return this.result;
|
||||
return this.result.join("");
|
||||
}
|
||||
|
||||
setString(name: string, value: string): void {
|
||||
@ -44,7 +44,6 @@ export class JSONEncoder {
|
||||
this.writeKey(name);
|
||||
this.write("[");
|
||||
this.isFirstKey = true
|
||||
this.inObject.push(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -56,7 +55,6 @@ export class JSONEncoder {
|
||||
this.writeKey(name);
|
||||
this.write("{");
|
||||
this.isFirstKey = true
|
||||
this.inObject.push(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -116,6 +114,6 @@ export class JSONEncoder {
|
||||
}
|
||||
|
||||
private write(str: string): void {
|
||||
this.result += str;
|
||||
this.result.push(str);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user