lib: simplify code with String.prototype.repeat()

use String.prototype.repeat() to simplify code, less code,
more semantically.

PR-URL: https://github.com/nodejs/node/pull/5359
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Jackson Tian 2016-02-19 11:01:11 +08:00 committed by James M Snell
parent bac6c85056
commit 4d78121b77
14 changed files with 18 additions and 70 deletions

View File

@ -4,7 +4,7 @@ var http = require('http');
var port = parseInt(process.env.PORT || 8000); var port = parseInt(process.env.PORT || 8000);
var fixed = makeString(20 * 1024, 'C'), var fixed = 'C'.repeat(20 * 1024),
storedBytes = {}, storedBytes = {},
storedBuffer = {}, storedBuffer = {},
storedUnicode = {}; storedUnicode = {};
@ -42,7 +42,7 @@ var server = module.exports = http.createServer(function(req, res) {
if (n <= 0) if (n <= 0)
throw new Error('bytes called with n <= 0'); throw new Error('bytes called with n <= 0');
if (storedBytes[n] === undefined) { if (storedBytes[n] === undefined) {
storedBytes[n] = makeString(n, 'C'); storedBytes[n] = 'C'.repeat(n);
} }
body = storedBytes[n]; body = storedBytes[n];
@ -63,7 +63,7 @@ var server = module.exports = http.createServer(function(req, res) {
if (n <= 0) if (n <= 0)
throw new Error('unicode called with n <= 0'); throw new Error('unicode called with n <= 0');
if (storedUnicode[n] === undefined) { if (storedUnicode[n] === undefined) {
storedUnicode[n] = makeString(n, '\u263A'); storedUnicode[n] = '\u263A'.repeat(n);
} }
body = storedUnicode[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() { server.listen(port, function() {
if (module === require.main) if (module === require.main)
console.error('Listening at http://127.0.0.1:' + port + '/'); console.error('Listening at http://127.0.0.1:' + port + '/');

View File

@ -13,11 +13,7 @@ var spawn = require('child_process').spawn;
var port = parseInt(process.env.PORT || 8000); var port = parseInt(process.env.PORT || 8000);
var fixed = ''; var fixed = "C".repeat(20 * 1024);
var i;
for (i = 0; i < 20 * 1024; i++) {
fixed += 'C';
}
var stored = {}; var stored = {};
var storedBuffer = {}; var storedBuffer = {};
@ -36,10 +32,7 @@ var server = http.createServer(function(req, res) {
if (n <= 0) if (n <= 0)
throw new Error('bytes called with n <= 0'); throw new Error('bytes called with n <= 0');
if (stored[n] === undefined) { if (stored[n] === undefined) {
stored[n] = ''; stored[n] = "C".repeat(n);
for (i = 0; i < n; i++) {
stored[n] += 'C';
}
} }
body = stored[n]; body = stored[n];

View File

@ -9,10 +9,7 @@ var bytes = 1024 * 5;
var requests = 0; var requests = 0;
var responses = 0; var responses = 0;
var body = ''; var body = 'C'.repeat(bytes);
for (var i = 0; i < bytes; i++) {
body += 'C';
}
var server = http.createServer(function(req, res) { var server = http.createServer(function(req, res) {
res.writeHead(200, { res.writeHead(200, {

View File

@ -1016,11 +1016,7 @@ function leftPad(n, prefix, maxN) {
const nchars = Math.max(2, String(maxN).length) + 1; const nchars = Math.max(2, String(maxN).length) + 1;
const nspaces = nchars - s.length - 1; const nspaces = nchars - s.length - 1;
for (var i = 0; i < nspaces; i++) { return prefix + ' '.repeat(nspaces) + s;
prefix += ' ';
}
return prefix + s;
} }

View File

@ -2,9 +2,4 @@ var assert = require('assert');
var n = parseInt(process.argv[2]); var n = parseInt(process.argv[2]);
var s = ''; process.stdout.write('c'.repeat(n));
for (var i = 0; i < n; i++) {
s += 'c';
}
process.stdout.write(s);

View File

@ -681,10 +681,7 @@ assert.equal(Buffer('=bad'.repeat(1e4), 'base64').length, 0);
{ {
// Creating buffers larger than pool size. // Creating buffers larger than pool size.
const l = Buffer.poolSize + 5; const l = Buffer.poolSize + 5;
let s = ''; const s = 'h'.repeat(l);
for (let i = 0; i < l; i++) {
s += 'h';
}
const b = new Buffer(s); const b = new Buffer(s);

View File

@ -219,7 +219,7 @@ function test_cyclic_link_overprotection(callback) {
var folder = cycles + '/folder'; var folder = cycles + '/folder';
var link = folder + '/cycles'; var link = folder + '/cycles';
var testPath = 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) {} try {fs.unlinkSync(link);} catch (ex) {}
fs.symlinkSync(cycles, link, 'dir'); fs.symlinkSync(cycles, link, 'dir');
unlink.push(link); unlink.push(link);

View File

@ -7,10 +7,7 @@ var exec = require('child_process').exec;
var bodyLength = 12345; var bodyLength = 12345;
var body = ''; var body = 'c'.repeat(bodyLength);
for (var i = 0; i < bodyLength; i++) {
body += 'c';
}
var server = http.createServer(function(req, res) { var server = http.createServer(function(req, res) {
res.writeHead(200, { res.writeHead(200, {

View File

@ -22,9 +22,8 @@ var server = http.createServer(function(req, res) {
}).listen(common.PORT, function() { }).listen(common.PORT, function() {
const s = net.connect(common.PORT); const s = net.connect(common.PORT);
var big = ''; var big = 'GET / HTTP/1.0\r\n\r\n'.repeat(COUNT);
for (var i = 0; i < COUNT; i++)
big += 'GET / HTTP/1.0\r\n\r\n';
s.write(big); s.write(big);
s.resume(); s.resume();
}); });

View File

@ -4,10 +4,7 @@ var assert = require('assert');
var net = require('net'); var net = require('net');
var kPoolSize = 40 * 1024; var kPoolSize = 40 * 1024;
var data = ''; var data = 'あ'.repeat(kPoolSize);
for (var i = 0; i < kPoolSize; ++i) {
data += 'あ'; // 3bytes
}
var receivedSize = 0; var receivedSize = 0;
var encoding = 'UTF-8'; var encoding = 'UTF-8';

View File

@ -16,12 +16,9 @@ var options = {
}; };
var reqCount = 0; var reqCount = 0;
var body = '';
process.stdout.write('build body...'); process.stdout.write('build body...');
for (var i = 0; i < 1024 * 1024; i++) { var body = 'hello world\n'.repeat(1024 * 1024);
body += 'hello world\n';
}
process.stdout.write('done\n'); process.stdout.write('done\n');
var server = https.createServer(options, function(req, res) { var server = https.createServer(options, function(req, res) {

View File

@ -11,10 +11,7 @@ var connections_per_client = 5;
// measured // measured
var total_connections = 0; var total_connections = 0;
var body = ''; var body = 'C'.repeat(bytes);
for (var i = 0; i < bytes; i++) {
body += 'C';
}
var server = net.createServer(function(c) { var server = net.createServer(function(c) {
console.log('connected'); console.log('connected');

View File

@ -9,10 +9,7 @@ var chars_recved = 0;
var npauses = 0; var npauses = 0;
console.log('build big string'); console.log('build big string');
var body = ''; body = 'C'.repeat(N);
for (var i = 0; i < N; i++) {
body += 'C';
}
console.log('start server on port ' + common.PORT); console.log('start server on port ' + common.PORT);

View File

@ -12,16 +12,10 @@ if (!common.hasCrypto) {
var tls = require('tls'); var tls = require('tls');
var fs = require('fs'); var fs = require('fs');
var body = '';
process.stdout.write('build body...'); process.stdout.write('build body...');
for (var i = 0; i < 1024 * 1024; i++) { var body = 'hello world\n'.repeat(1024 * 1024);
body += 'hello world\n';
}
process.stdout.write('done\n'); process.stdout.write('done\n');
var options = { var options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'), key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem') cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem')