From 08130dd3b9b0f0497e4b09e9673e63dbe4e2559b Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Fri, 19 Feb 2016 11:01:11 +0800 Subject: [PATCH] lib,test,benchmark: simplify code with String.prototype.repeat() use String.prototype.repeat() to simplify code, less code, more semantically. --- benchmark/http_simple.js | 14 +++----------- benchmark/http_simple_auto.js | 11 ++--------- benchmark/static_http_server.js | 5 +---- lib/_debugger.js | 6 +----- test/fixtures/print-chars.js | 7 +------ test/parallel/test-buffer.js | 5 +---- test/parallel/test-fs-realpath.js | 2 +- test/parallel/test-http-full-response.js | 5 +---- test/parallel/test-http-pipeline-regr-2639.js | 5 ++--- test/parallel/test-net-large-string.js | 5 +---- test/pummel/test-https-large-response.js | 5 +---- test/pummel/test-net-many-clients.js | 5 +---- test/pummel/test-net-throttle.js | 5 +---- test/pummel/test-tls-throttle.js | 8 +------- 14 files changed, 18 insertions(+), 70 deletions(-) diff --git a/benchmark/http_simple.js b/benchmark/http_simple.js index 0f36d3b6ce3ebd..9a580de2c0f665 100644 --- a/benchmark/http_simple.js +++ b/benchmark/http_simple.js @@ -4,7 +4,7 @@ var http = require('http'); var port = parseInt(process.env.PORT || 8000); -var fixed = makeString(20 * 1024, 'C'), +var fixed = 'C'.repeat(20 * 1024), storedBytes = {}, storedBuffer = {}, storedUnicode = {}; @@ -42,7 +42,7 @@ var server = module.exports = http.createServer(function(req, res) { if (n <= 0) throw new Error('bytes called with n <= 0'); if (storedBytes[n] === undefined) { - storedBytes[n] = makeString(n, 'C'); + storedBytes[n] = 'C'.repeat(n); } body = storedBytes[n]; @@ -63,7 +63,7 @@ var server = module.exports = http.createServer(function(req, res) { if (n <= 0) throw new Error('unicode called with n <= 0'); if (storedUnicode[n] === undefined) { - storedUnicode[n] = makeString(n, '\u263A'); + storedUnicode[n] = '\u263A'.repeat(n); } body = storedUnicode[n]; @@ -107,14 +107,6 @@ var server = module.exports = http.createServer(function(req, res) { } }); -function makeString(size, c) { - var s = ''; - while (s.length < size) { - s += c; - } - return s; -} - server.listen(port, function() { if (module === require.main) console.error('Listening at http://127.0.0.1:' + port + '/'); diff --git a/benchmark/http_simple_auto.js b/benchmark/http_simple_auto.js index 1f3c8e7612e54b..1604a5286335bd 100644 --- a/benchmark/http_simple_auto.js +++ b/benchmark/http_simple_auto.js @@ -13,11 +13,7 @@ var spawn = require('child_process').spawn; var port = parseInt(process.env.PORT || 8000); -var fixed = ''; -var i; -for (i = 0; i < 20 * 1024; i++) { - fixed += 'C'; -} +var fixed = "C".repeat(20 * 1024); var stored = {}; var storedBuffer = {}; @@ -36,10 +32,7 @@ var server = http.createServer(function(req, res) { if (n <= 0) throw new Error('bytes called with n <= 0'); if (stored[n] === undefined) { - stored[n] = ''; - for (i = 0; i < n; i++) { - stored[n] += 'C'; - } + stored[n] = "C".repeat(n); } body = stored[n]; diff --git a/benchmark/static_http_server.js b/benchmark/static_http_server.js index 804f83c208bb39..43c493675933e7 100644 --- a/benchmark/static_http_server.js +++ b/benchmark/static_http_server.js @@ -9,10 +9,7 @@ var bytes = 1024 * 5; var requests = 0; var responses = 0; -var body = ''; -for (var i = 0; i < bytes; i++) { - body += 'C'; -} +var body = 'C'.repeat(bytes); var server = http.createServer(function(req, res) { res.writeHead(200, { diff --git a/lib/_debugger.js b/lib/_debugger.js index 7ec91d1e002c22..b0e250c293560c 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -1016,11 +1016,7 @@ function leftPad(n, prefix, maxN) { const nchars = Math.max(2, String(maxN).length) + 1; const nspaces = nchars - s.length - 1; - for (var i = 0; i < nspaces; i++) { - prefix += ' '; - } - - return prefix + s; + return prefix + ' '.repeat(nspaces) + s; } diff --git a/test/fixtures/print-chars.js b/test/fixtures/print-chars.js index e2223e20f25508..b9707fbf75fa67 100644 --- a/test/fixtures/print-chars.js +++ b/test/fixtures/print-chars.js @@ -2,9 +2,4 @@ var assert = require('assert'); var n = parseInt(process.argv[2]); -var s = ''; -for (var i = 0; i < n; i++) { - s += 'c'; -} - -process.stdout.write(s); +process.stdout.write('c'.repeat(n)); diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 8d42bea82d5e8c..a9c34cfa26ac85 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -681,10 +681,7 @@ assert.equal(Buffer('=bad'.repeat(1e4), 'base64').length, 0); { // Creating buffers larger than pool size. const l = Buffer.poolSize + 5; - let s = ''; - for (let i = 0; i < l; i++) { - s += 'h'; - } + const s = 'h'.repeat(l); const b = new Buffer(s); diff --git a/test/parallel/test-fs-realpath.js b/test/parallel/test-fs-realpath.js index dcc28ce4d99afa..aa397e51d16409 100644 --- a/test/parallel/test-fs-realpath.js +++ b/test/parallel/test-fs-realpath.js @@ -219,7 +219,7 @@ function test_cyclic_link_overprotection(callback) { var folder = cycles + '/folder'; var link = folder + '/cycles'; var testPath = cycles; - for (var i = 0; i < 10; i++) testPath += '/folder/cycles'; + testPath += '/folder/cycles'.repeat(10); try {fs.unlinkSync(link);} catch (ex) {} fs.symlinkSync(cycles, link, 'dir'); unlink.push(link); diff --git a/test/parallel/test-http-full-response.js b/test/parallel/test-http-full-response.js index 8472a84fb9fac9..767efa3cd6657e 100644 --- a/test/parallel/test-http-full-response.js +++ b/test/parallel/test-http-full-response.js @@ -7,10 +7,7 @@ var exec = require('child_process').exec; var bodyLength = 12345; -var body = ''; -for (var i = 0; i < bodyLength; i++) { - body += 'c'; -} +var body = 'c'.repeat(bodyLength); var server = http.createServer(function(req, res) { res.writeHead(200, { diff --git a/test/parallel/test-http-pipeline-regr-2639.js b/test/parallel/test-http-pipeline-regr-2639.js index f1f75bff650ac5..c7cebb9d46a19f 100644 --- a/test/parallel/test-http-pipeline-regr-2639.js +++ b/test/parallel/test-http-pipeline-regr-2639.js @@ -22,9 +22,8 @@ var server = http.createServer(function(req, res) { }).listen(common.PORT, function() { const s = net.connect(common.PORT); - var big = ''; - for (var i = 0; i < COUNT; i++) - big += 'GET / HTTP/1.0\r\n\r\n'; + var big = 'GET / HTTP/1.0\r\n\r\n'.repeat(COUNT); + s.write(big); s.resume(); }); diff --git a/test/parallel/test-net-large-string.js b/test/parallel/test-net-large-string.js index 387f3f4f8c1dd3..26227c8e759787 100644 --- a/test/parallel/test-net-large-string.js +++ b/test/parallel/test-net-large-string.js @@ -4,10 +4,7 @@ var assert = require('assert'); var net = require('net'); var kPoolSize = 40 * 1024; -var data = ''; -for (var i = 0; i < kPoolSize; ++i) { - data += 'あ'; // 3bytes -} +var data = 'あ'.repeat(kPoolSize); var receivedSize = 0; var encoding = 'UTF-8'; diff --git a/test/pummel/test-https-large-response.js b/test/pummel/test-https-large-response.js index 4a15b29cd19467..1979c6737f76b3 100644 --- a/test/pummel/test-https-large-response.js +++ b/test/pummel/test-https-large-response.js @@ -16,12 +16,9 @@ var options = { }; var reqCount = 0; -var body = ''; process.stdout.write('build body...'); -for (var i = 0; i < 1024 * 1024; i++) { - body += 'hello world\n'; -} +var body = 'hello world\n'.repeat(1024 * 1024); process.stdout.write('done\n'); var server = https.createServer(options, function(req, res) { diff --git a/test/pummel/test-net-many-clients.js b/test/pummel/test-net-many-clients.js index 6d9dde1ddbc863..cb766ec1a47c63 100644 --- a/test/pummel/test-net-many-clients.js +++ b/test/pummel/test-net-many-clients.js @@ -11,10 +11,7 @@ var connections_per_client = 5; // measured var total_connections = 0; -var body = ''; -for (var i = 0; i < bytes; i++) { - body += 'C'; -} +var body = 'C'.repeat(bytes); var server = net.createServer(function(c) { console.log('connected'); diff --git a/test/pummel/test-net-throttle.js b/test/pummel/test-net-throttle.js index 54956b789aed93..e98a6c765a905e 100644 --- a/test/pummel/test-net-throttle.js +++ b/test/pummel/test-net-throttle.js @@ -9,10 +9,7 @@ var chars_recved = 0; var npauses = 0; console.log('build big string'); -var body = ''; -for (var i = 0; i < N; i++) { - body += 'C'; -} +body = 'C'.repeat(N); console.log('start server on port ' + common.PORT); diff --git a/test/pummel/test-tls-throttle.js b/test/pummel/test-tls-throttle.js index 67efb61b7e66aa..0e5cd455c8c4c4 100644 --- a/test/pummel/test-tls-throttle.js +++ b/test/pummel/test-tls-throttle.js @@ -12,16 +12,10 @@ if (!common.hasCrypto) { var tls = require('tls'); var fs = require('fs'); - -var body = ''; - process.stdout.write('build body...'); -for (var i = 0; i < 1024 * 1024; i++) { - body += 'hello world\n'; -} +var body = 'hello world\n'.repeat(1024 * 1024); process.stdout.write('done\n'); - var options = { key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'), cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem')