From 6266ec0fedd2adbbf215c3f1f449ef0ccc65e91e Mon Sep 17 00:00:00 2001 From: Merijn Hendriks Date: Tue, 28 Dec 2021 18:07:09 +0100 Subject: [PATCH] reuse TextEncoder across instances --- src/common/Haru.Http.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/common/Haru.Http.ts b/src/common/Haru.Http.ts index 39446a5..b2d8203 100644 --- a/src/common/Haru.Http.ts +++ b/src/common/Haru.Http.ts @@ -114,20 +114,19 @@ export class HttpServer implements IServer export class Service { + private static readonly textEncoder = new TextEncoder() + protected send(res: ServerResponse, data: Uint8Array, type: string, compress: boolean = true): void { const bytes = (compress) ? Zlib.compress(data) : data; - const headers = { - "Content-Type": Mime.types[type] || Mime.types["bin"] - }; - + const headers = { "Content-Type": Mime.types[type] || Mime.types["bin"] }; res.writeHead(200, "OK", headers); res.end(bytes); } protected sendJson(res: ServerResponse, data: string, compress: boolean = true): void { - const bytes = new TextEncoder().encode(data); + const bytes = Service.textEncoder.encode(data); this.send(res, bytes, "json", compress); }