From 7b2ddb4ca6824eafe7a8943eab1ea1670c9dcf60 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 8 Jun 2017 02:00:33 +0300 Subject: [PATCH 1/3] libs: take RegExp creation out of cycles --- lib/dns.js | 6 ++++-- lib/internal/cluster/master.js | 6 +++--- lib/repl.js | 3 ++- lib/util.js | 8 +++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/dns.js b/lib/dns.js index 6fcf37a85e6c12..4ef230e5739a44 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -309,13 +309,15 @@ function setServers(servers) { // servers cares won't have any servers available for resolution const orig = cares.getServers(); const newSet = []; + const IPv6RE = /\[(.*)\](?::\d+)?/; + const addrSplitRE = /:\d+$/; servers.forEach((serv) => { var ipVersion = isIP(serv); if (ipVersion !== 0) return newSet.push([ipVersion, serv]); - const match = serv.match(/\[(.*)\](?::\d+)?/); + const match = serv.match(IPv6RE); // we have an IPv6 in brackets if (match) { ipVersion = isIP(match[1]); @@ -323,7 +325,7 @@ function setServers(servers) { return newSet.push([ipVersion, match[1]]); } - const s = serv.split(/:\d+$/)[0]; + const s = serv.split(addrSplitRE)[0]; ipVersion = isIP(s); if (ipVersion !== 0) diff --git a/lib/internal/cluster/master.js b/lib/internal/cluster/master.js index b20a27c5ee5129..05bba01c4ed6e8 100644 --- a/lib/internal/cluster/master.js +++ b/lib/internal/cluster/master.js @@ -99,14 +99,14 @@ function createWorkerProcess(id, env) { var workerEnv = util._extend({}, process.env); var execArgv = cluster.settings.execArgv.slice(); var debugPort = 0; + var debugArgvRE = + /^(--inspect|--inspect-(brk|port)|--debug|--debug-(brk|port))(=\d+)?$/; util._extend(workerEnv, env); workerEnv.NODE_UNIQUE_ID = '' + id; for (var i = 0; i < execArgv.length; i++) { - const match = execArgv[i].match( - /^(--inspect|--inspect-(brk|port)|--debug|--debug-(brk|port))(=\d+)?$/ - ); + const match = execArgv[i].match(debugArgvRE); if (match) { if (debugPort === 0) { diff --git a/lib/repl.js b/lib/repl.js index 051261a679ebeb..bb51a33b4fcef2 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -754,6 +754,7 @@ function complete(line, callback) { const exts = Object.keys(this.context.require.extensions); var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') + ')$'); + var versionedFileNamesRe = /-\d+\.\d+(\.\d+)?/; completeOn = match[1]; var subdir = match[2] || ''; @@ -772,7 +773,7 @@ function complete(line, callback) { name = files[f]; ext = path.extname(name); base = name.slice(0, -ext.length); - if (base.match(/-\d+\.\d+(\.\d+)?/) || name === '.npm') { + if (base.match(versionedFileNamesRe) || name === '.npm') { // Exclude versioned names that 'npm' installs. continue; } diff --git a/lib/util.js b/lib/util.js index 91e17cb651c0f9..98656b1bc25ca0 100644 --- a/lib/util.js +++ b/lib/util.js @@ -39,6 +39,8 @@ const inspectDefaultOptions = Object.seal({ breakLength: 60 }); +const numbersOnlyRE = /^\d+$/; + var CIRCULAR_ERROR_MESSAGE; var Debug; @@ -664,7 +666,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { } for (var n = 0; n < keys.length; n++) { var key = keys[n]; - if (typeof key === 'symbol' || !key.match(/^\d+$/)) { + if (typeof key === 'symbol' || !key.match(numbersOnlyRE)) { output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, key, true)); } @@ -683,7 +685,7 @@ function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) { output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); } for (const key of keys) { - if (typeof key === 'symbol' || !key.match(/^\d+$/)) { + if (typeof key === 'symbol' || !key.match(numbersOnlyRE)) { output.push( formatProperty(ctx, value, recurseTimes, visibleKeys, key, true)); } @@ -798,7 +800,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { } } if (name === undefined) { - if (array && key.match(/^\d+$/)) { + if (array && key.match(numbersOnlyRE)) { return str; } name = JSON.stringify('' + key); From 67f74d58d3cf37f78107f28d853a199a98cef0a3 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 8 Jun 2017 02:07:28 +0300 Subject: [PATCH 2/3] libs: use test(), not match() in boolean context --- lib/_tls_wrap.js | 2 +- lib/repl.js | 4 ++-- lib/util.js | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 23a5c2840e6e3e..eba8aada150bd2 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -963,7 +963,7 @@ function SNICallback(servername, callback) { var ctx; this.server._contexts.some(function(elem) { - if (servername.match(elem[0]) !== null) { + if (elem[0].test(servername)) { ctx = elem[1]; return true; } diff --git a/lib/repl.js b/lib/repl.js index bb51a33b4fcef2..59747c4e5f0263 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -773,7 +773,7 @@ function complete(line, callback) { name = files[f]; ext = path.extname(name); base = name.slice(0, -ext.length); - if (base.match(versionedFileNamesRe) || name === '.npm') { + if (versionedFileNamesRe.test(base) || name === '.npm') { // Exclude versioned names that 'npm' installs. continue; } @@ -817,7 +817,7 @@ function complete(line, callback) { // spam.eggs.<|> # completions for 'spam.eggs' with filter '' // foo<|> # all scope vars with filter 'foo' // foo.<|> # completions for 'foo' with filter '' - } else if (line.length === 0 || line[line.length - 1].match(/\w|\.|\$/)) { + } else if (line.length === 0 || /\w|\.|\$/.test(line[line.length - 1])) { match = simpleExpressionRE.exec(line); if (line.length === 0 || match) { var expr; diff --git a/lib/util.js b/lib/util.js index 98656b1bc25ca0..3c31e0932a6672 100644 --- a/lib/util.js +++ b/lib/util.js @@ -666,7 +666,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { } for (var n = 0; n < keys.length; n++) { var key = keys[n]; - if (typeof key === 'symbol' || !key.match(numbersOnlyRE)) { + if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) { output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, key, true)); } @@ -685,7 +685,7 @@ function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) { output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); } for (const key of keys) { - if (typeof key === 'symbol' || !key.match(numbersOnlyRE)) { + if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) { output.push( formatProperty(ctx, value, recurseTimes, visibleKeys, key, true)); } @@ -800,11 +800,11 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { } } if (name === undefined) { - if (array && key.match(numbersOnlyRE)) { + if (array && numbersOnlyRE.test(key)) { return str; } name = JSON.stringify('' + key); - if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { + if (/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/.test(name)) { name = name.substr(1, name.length - 2); name = ctx.stylize(name, 'name'); } else { From a48371ca8720b4ba5d4d66e122d1c6b583349f17 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 8 Jun 2017 02:10:38 +0300 Subject: [PATCH 3/3] libs: remove redundant RegExp parts --- lib/dns.js | 2 +- lib/repl.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/dns.js b/lib/dns.js index 4ef230e5739a44..81f40b44bddc99 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -309,7 +309,7 @@ function setServers(servers) { // servers cares won't have any servers available for resolution const orig = cares.getServers(); const newSet = []; - const IPv6RE = /\[(.*)\](?::\d+)?/; + const IPv6RE = /\[(.*)\]/; const addrSplitRE = /:\d+$/; servers.forEach((serv) => { diff --git a/lib/repl.js b/lib/repl.js index 59747c4e5f0263..b36af19d797c7c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -754,7 +754,7 @@ function complete(line, callback) { const exts = Object.keys(this.context.require.extensions); var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') + ')$'); - var versionedFileNamesRe = /-\d+\.\d+(\.\d+)?/; + var versionedFileNamesRe = /-\d+\.\d+/; completeOn = match[1]; var subdir = match[2] || ''; @@ -1068,7 +1068,7 @@ REPLServer.prototype.memory = function memory(cmd) { self.lines.level.push({ line: self.lines.length - 1, depth: depth, - isFunction: /\s*function\s*/.test(cmd) + isFunction: /\bfunction\b/.test(cmd) }); } else if (depth < 0) { // going... up.