From 4b921188785fa17203794836a3c66130afd0ed4f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 5 Sep 2020 15:32:06 +0200 Subject: [PATCH] perf: reduce string ops (#402) --- lib/core/client.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/core/client.js b/lib/core/client.js index 31fc2f79ab9..587c4408121 100644 --- a/lib/core/client.js +++ b/lib/core/client.js @@ -56,6 +56,8 @@ const { kTLSSession } = require('./symbols') +const kHostHeader = Symbol('host header') + const nodeMajorVersion = parseInt(process.version.split('.')[0].slice(1)) const insecureHTTPParser = process.execArgv.includes('--insecure-http-parser') @@ -161,6 +163,7 @@ class Client extends EventEmitter { } } this[kTLSSession] = null + this[kHostHeader] = `host: ${this[kUrl].hostname}\r\n` // kQueue is built up of 3 sections separated by // the kRunningIdx and kPendingIdx indices. @@ -993,18 +996,18 @@ function write (client, request) { // or follow a request with an extra CRLF. // https://tools.ietf.org/html/rfc7230#section-3.5 - let header = `${method} ${path} HTTP/1.1\r\n` + let header if (upgrade) { - header += `connection: upgrade\r\nupgrade: ${upgrade}\r\n` + header = `${method} ${path} HTTP/1.1\r\nconnection: upgrade\r\nupgrade: ${upgrade}\r\n` } else if (client[kKeepAlive]) { - header += 'connection: keep-alive\r\n' + header = `${method} ${path} HTTP/1.1\r\nconnection: keep-alive\r\n` } else { - header += 'connection: close\r\n' + header = `${method} ${path} HTTP/1.1\r\nconnection: close\r\n` } if (!host) { - header += `host: ${client[kUrl].hostname}\r\n` + header += client[kHostHeader] } if (headers) {