diff --git a/benchmark/http/set_header.js b/benchmark/http/set_header.js new file mode 100644 index 00000000000000..22de61b3f847a3 --- /dev/null +++ b/benchmark/http/set_header.js @@ -0,0 +1,30 @@ +'use strict'; + +const common = require('../common.js'); +const { OutgoingMessage } = require('_http_outgoing'); + +const bench = common.createBenchmark(main, { + value: [ + 'X-Powered-By', + 'Vary', + 'Set-Cookie', + 'Content-Type', + 'Content-Length', + 'Connection', + 'Transfer-Encoding' + ], + n: [1e6], +}); + +function main(conf) { + const n = +conf.n; + const value = conf.value; + + const og = new OutgoingMessage(); + + bench.start(); + for (var i = 0; i < n; i++) { + og.setHeader(value, ''); + } + bench.end(n); +} diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 79d38db291a170..5f4e8e2cf14b96 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -510,18 +510,15 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) { const key = name.toLowerCase(); this[outHeadersKey][key] = [name, value]; - switch (key.length) { - case 10: - if (key === 'connection') - this._removedConnection = false; + switch (key) { + case 'connection': + this._removedConnection = false; break; - case 14: - if (key === 'content-length') - this._removedContLen = false; + case 'content-length': + this._removedContLen = false; break; - case 17: - if (key === 'transfer-encoding') - this._removedTE = false; + case 'transfer-encoding': + this._removedTE = false; break; } }; @@ -583,22 +580,18 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) { var key = name.toLowerCase(); - switch (key.length) { - case 10: - if (key === 'connection') - this._removedConnection = true; + switch (key) { + case 'connection': + this._removedConnection = true; break; - case 14: - if (key === 'content-length') - this._removedContLen = true; + case 'content-length': + this._removedContLen = true; break; - case 17: - if (key === 'transfer-encoding') - this._removedTE = true; + case 'transfer-encoding': + this._removedTE = true; break; - case 4: - if (key === 'date') - this.sendDate = false; + case 'date': + this.sendDate = false; break; }