From d76403985f33050256acb93500c31380cf88be11 Mon Sep 17 00:00:00 2001 From: Seth Brenith Date: Tue, 23 Jan 2018 16:04:17 -0800 Subject: [PATCH] http: switch on string values Long ago, V8 was much faster switching on string lengths than values. That is no longer the case, so we can simplify a couple of methods. PR-URL: https://github.com/nodejs/node/pull/18351 Reviewed-By: James M Snell Reviewed-By: Kyle Farnung Reviewed-By: Anatoli Papirovski Reviewed-By: Gireesh Punathil Reviewed-By: Colin Ihrig Reviewed-By: Jon Moss --- benchmark/http/set_header.js | 30 +++++++++++++++++++++++++++ lib/_http_outgoing.js | 39 +++++++++++++++--------------------- 2 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 benchmark/http/set_header.js 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 8fecfc8a8db878..58d0753cffdb3e 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -515,18 +515,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; } }; @@ -588,22 +585,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; }