Skip to content

Commit

Permalink
add benchmark for parse-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Mar 15, 2017
1 parent 88cc35b commit e3b2fe1
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions parse-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'use strict';

function intern(str) {
let obj = {};
obj[str] = 1;
for (let key in obj) {
if (key === str) {
return key;
}
}
return str;
}

function _toArray(arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
}

var CLRF = '\r\n';
function parseResponseHeaders(headersString) {
var headers = Object.create(null);

if (!headersString) {
return headers;
}

var headerPairs = headersString.split(CLRF);

headerPairs.forEach(function (header) {
var _header$split = header.split(':');

var _header$split2 = _header$split;

var field = _header$split2[0];

var value = _header$split2.slice(1);

field = field.trim();
value = value.join(':').trim();

if (value) {
headers[field] = value;
}
});

return headers;
}

var HEADER_MATCH = /^([^:]*):(.*)$/m;

function parseResponseHeadersNew(headersString) {
var headers = Object.create(null);

if (!headersString) {
return headers;
}

var headerPairs = headersString.split(CLRF);
for (var i = 0; i < headerPairs.length; i++) {
var header = headerPairs[i];
var j = 0;
var foundColon = false;

for (; j < header.length; j++) {
if (header.charCodeAt(j) === 58) {
foundColon = true;
break;
}
}

if (foundColon === false) {
break;
}

var field = header.substring(0, j).trim();
var value = header.substring(j + 1, header.length).trim();

if (value) {
headers[field] = value;
}
}

return headers;
}

// I don't believe they are ropes when we get them from the platform
const HEADER = intern([
'Date: Wed, 15 Mar 2017 15:57:19 GMT',
'Cache-Control: private, max-age=0, must-revalidate',
'Last-Modified: Tue, 14 Mar 2017 21:25:26 GMT',
'Content-Encoding: gzip',
'X-Powered-By: Express',
'Vary: Accept-Encoding',
'Content-Type: application/json'
].join(CLRF));

console.log(parseResponseHeaders(HEADER));
console.log(parseResponseHeadersNew(HEADER));
require('do-you-even-bench')([
{
name: 'new',
fn() {
parseResponseHeadersNew(HEADER);
}
},
{
name: 'current',
fn() {
parseResponseHeaders(HEADER);
}
}
])

0 comments on commit e3b2fe1

Please sign in to comment.