Skip to content

Commit

Permalink
http: switch on string values
Browse files Browse the repository at this point in the history
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: #18351
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
  • Loading branch information
sethbrenith authored and MylesBorins committed Feb 21, 2018
1 parent b5267a6 commit d764039
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
30 changes: 30 additions & 0 deletions benchmark/http/set_header.js
Original file line number Diff line number Diff line change
@@ -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);
}
39 changes: 16 additions & 23 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit d764039

Please sign in to comment.