Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http/server : Header parsing perf enhancement #337

Closed
wants to merge 1 commit into from

Conversation

2 participants
@zekth
Copy link
Contributor

zekth commented Apr 13, 2019

Just changed the header parsing way to use an array instead of string concat. This speeds up 0.02ms earch request in average.

Here is the test code to use with --allow-high-precision

let s, e;
let headers: Headers = new Headers();
for (let i = 0; i < 10; i++) {
  headers.append(
    "head-" + i,
    Math.random()
      .toString(36)
      .substring(7)
  );
}
const average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
const str = [];
const arr = [];
for (let i = 0; i < 500; i++) {
  s = performance.now();
  let out = "";
  if (headers) {
    for (const [key, value] of headers) {
      out += `${key}: ${value}\r\n`;
    }
  }
  out += "\r\n";
  e = performance.now();
  str.push(e - s);

  s = performance.now();
  let outArr = [];
  if (headers) {
    headers.forEach((v,k) => outArr.push(`${k}: ${v}\r\n`))
  }
  outArr.push("\r\n");
  outArr.join("");
  e = performance.now();
  arr.push(e - s);

}
console.log(`String implementation: ${average(str)}ms`);
console.log(`Array implementation: ${average(arr)}ms`);

// String implementation: 0.07737456799999913ms
// Array implementation: 0.05701406000000017ms
@ry

This comment has been minimized.

Copy link
Contributor

ry commented Apr 14, 2019

See my comment here: #338 (comment)
If you can move the numbers in the benchmark, I'm interested. But this will not make a difference.
I appreciate the patch but closing without merge.

@ry ry closed this Apr 14, 2019

@zekth zekth deleted the zekth:perf-improvement branch Apr 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.