GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — tls: avoid calling Buffer.byteLength multiple times · nodejs/node@77331a7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/tls
    • convertprotocols.js
  • lib
    • tls.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/tls/convertprotocols.js‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const tls = require('tls');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1, 50000]
8+
});
9+
10+
function main(conf) {
11+
const n = +conf.n;
12+
13+
var i = 0;
14+
var m = {};
15+
common.v8ForceOptimization(
16+
tls.convertNPNProtocols, ['ABC', 'XYZ123', 'FOO'], m);
17+
bench.start();
18+
for (; i < n; i++) tls.convertNPNProtocols(['ABC', 'XYZ123', 'FOO'], m);
19+
bench.end(n);
20+
}
Collapse file

‎lib/tls.js‎

Copy file name to clipboardExpand all lines: lib/tls.js
+11-9Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ exports.getCiphers = internalUtil.cachedResult(() => {
2929
// Convert protocols array into valid OpenSSL protocols list
3030
// ("\x06spdy/2\x08http/1.1\x08http/1.0")
3131
function convertProtocols(protocols) {
32-
var buff = Buffer.allocUnsafe(protocols.reduce(function(p, c) {
33-
return p + 1 + Buffer.byteLength(c);
32+
const lens = Array(protocols.length);
33+
const buff = Buffer.allocUnsafe(protocols.reduce((p, c, i) => {
34+
var len = Buffer.byteLength(c);
35+
lens[i] = len;
36+
return p + 1 + len;
3437
}, 0));
3538

36-
protocols.reduce(function(offset, c) {
37-
var clen = Buffer.byteLength(c);
38-
buff[offset] = clen;
39-
buff.write(c, offset + 1);
40-
41-
return offset + 1 + clen;
42-
}, 0);
39+
var offset = 0;
40+
for (var i = 0, c = protocols.length; i < c; i++) {
41+
buff[offset++] = lens[i];
42+
buff.write(protocols[i], offset);
43+
offset += lens[i];
44+
}
4345

4446
return buff;
4547
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.