Skip to content

Commit d764039

Browse files
sethbrenithMylesBorins
authored andcommitted
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: #18351 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Kyle Farnung <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jon Moss <[email protected]>
1 parent b5267a6 commit d764039

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

benchmark/http/set_header.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const { OutgoingMessage } = require('_http_outgoing');
5+
6+
const bench = common.createBenchmark(main, {
7+
value: [
8+
'X-Powered-By',
9+
'Vary',
10+
'Set-Cookie',
11+
'Content-Type',
12+
'Content-Length',
13+
'Connection',
14+
'Transfer-Encoding'
15+
],
16+
n: [1e6],
17+
});
18+
19+
function main(conf) {
20+
const n = +conf.n;
21+
const value = conf.value;
22+
23+
const og = new OutgoingMessage();
24+
25+
bench.start();
26+
for (var i = 0; i < n; i++) {
27+
og.setHeader(value, '');
28+
}
29+
bench.end(n);
30+
}

lib/_http_outgoing.js

+16-23
Original file line numberDiff line numberDiff line change
@@ -515,18 +515,15 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
515515
const key = name.toLowerCase();
516516
this[outHeadersKey][key] = [name, value];
517517

518-
switch (key.length) {
519-
case 10:
520-
if (key === 'connection')
521-
this._removedConnection = false;
518+
switch (key) {
519+
case 'connection':
520+
this._removedConnection = false;
522521
break;
523-
case 14:
524-
if (key === 'content-length')
525-
this._removedContLen = false;
522+
case 'content-length':
523+
this._removedContLen = false;
526524
break;
527-
case 17:
528-
if (key === 'transfer-encoding')
529-
this._removedTE = false;
525+
case 'transfer-encoding':
526+
this._removedTE = false;
530527
break;
531528
}
532529
};
@@ -588,22 +585,18 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
588585

589586
var key = name.toLowerCase();
590587

591-
switch (key.length) {
592-
case 10:
593-
if (key === 'connection')
594-
this._removedConnection = true;
588+
switch (key) {
589+
case 'connection':
590+
this._removedConnection = true;
595591
break;
596-
case 14:
597-
if (key === 'content-length')
598-
this._removedContLen = true;
592+
case 'content-length':
593+
this._removedContLen = true;
599594
break;
600-
case 17:
601-
if (key === 'transfer-encoding')
602-
this._removedTE = true;
595+
case 'transfer-encoding':
596+
this._removedTE = true;
603597
break;
604-
case 4:
605-
if (key === 'date')
606-
this.sendDate = false;
598+
case 'date':
599+
this.sendDate = false;
607600
break;
608601
}
609602

0 commit comments

Comments
 (0)