diff options
author | James M Snell <jasnell@gmail.com> | 2016-06-09 01:47:18 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2016-06-21 20:53:25 +0300 |
commit | f3d5efa3ee4432121f6e2a1866f1179b09056594 (patch) | |
tree | 87e18ee9956c8c30d53b06bcfea4fcb1409f4367 /lib/tls.js | |
parent | 6be73feaeb31dbcc1547da648341c0b6f0d2d2bb (diff) |
tls: avoid calling Buffer.byteLength multiple times
There's no reason to be calling Buffer.byteLength() twice.
Small perf improvement
Run 1:
tls/convertprotocols.js n=1 v6.2.1 = 11852, new = 12204 ...... -2.89%
tls/convertprotocols.js n=50000 v6.2.1 = 515660, new = 570610 ..... -9.63%
Run 2:
tls/convertprotocols.js n=1 v6.2.1 = 11729, new = 12045 ...... -2.62%
tls/convertprotocols.js n=50000 v6.2.1 = 512080, new = 637730 ..... -19.70%
PR-URL: https://github.com/nodejs/node/pull/7236
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Diffstat (limited to 'lib/tls.js')
-rw-r--r-- | lib/tls.js | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/lib/tls.js b/lib/tls.js index 695edd8c5a6..849daeb07f3 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -29,17 +29,19 @@ exports.getCiphers = internalUtil.cachedResult(() => { // Convert protocols array into valid OpenSSL protocols list // ("\x06spdy/2\x08http/1.1\x08http/1.0") function convertProtocols(protocols) { - var buff = Buffer.allocUnsafe(protocols.reduce(function(p, c) { - return p + 1 + Buffer.byteLength(c); + const lens = Array(protocols.length); + const buff = Buffer.allocUnsafe(protocols.reduce((p, c, i) => { + var len = Buffer.byteLength(c); + lens[i] = len; + return p + 1 + len; }, 0)); - protocols.reduce(function(offset, c) { - var clen = Buffer.byteLength(c); - buff[offset] = clen; - buff.write(c, offset + 1); - - return offset + 1 + clen; - }, 0); + var offset = 0; + for (var i = 0, c = protocols.length; i < c; i++) { + buff[offset++] = lens[i]; + buff.write(protocols[i], offset); + offset += lens[i]; + } return buff; } |