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
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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
Copy file name to clipboardExpand all lines: lib/tls.js +11-9Lines changed: 11 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,17 +29,19 @@ exports.getCiphers = internalUtil.cachedResult(() => { |
29 | 29 | // Convert protocols array into valid OpenSSL protocols list |
30 | 30 | // ("\x06spdy/2\x08http/1.1\x08http/1.0") |
31 | 31 | 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; |
34 | 37 | }, 0)); |
35 | 38 |
|
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 | + } |
43 | 45 |
|
44 | 46 | return buff; |
45 | 47 | } |
|