Skip to content

Commit aba6bc3

Browse files
sethbrenithmaclover7
authored andcommittedJan 26, 2018
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 6c1906a commit aba6bc3

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
@@ -510,18 +510,15 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
510510
const key = name.toLowerCase();
511511
this[outHeadersKey][key] = [name, value];
512512

513-
switch (key.length) {
514-
case 10:
515-
if (key === 'connection')
516-
this._removedConnection = false;
513+
switch (key) {
514+
case 'connection':
515+
this._removedConnection = false;
517516
break;
518-
case 14:
519-
if (key === 'content-length')
520-
this._removedContLen = false;
517+
case 'content-length':
518+
this._removedContLen = false;
521519
break;
522-
case 17:
523-
if (key === 'transfer-encoding')
524-
this._removedTE = false;
520+
case 'transfer-encoding':
521+
this._removedTE = false;
525522
break;
526523
}
527524
};
@@ -583,22 +580,18 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
583580

584581
var key = name.toLowerCase();
585582

586-
switch (key.length) {
587-
case 10:
588-
if (key === 'connection')
589-
this._removedConnection = true;
583+
switch (key) {
584+
case 'connection':
585+
this._removedConnection = true;
590586
break;
591-
case 14:
592-
if (key === 'content-length')
593-
this._removedContLen = true;
587+
case 'content-length':
588+
this._removedContLen = true;
594589
break;
595-
case 17:
596-
if (key === 'transfer-encoding')
597-
this._removedTE = true;
590+
case 'transfer-encoding':
591+
this._removedTE = true;
598592
break;
599-
case 4:
600-
if (key === 'date')
601-
this.sendDate = false;
593+
case 'date':
594+
this.sendDate = false;
602595
break;
603596
}
604597

0 commit comments

Comments
 (0)
Please sign in to comment.