From 04c3fdc9fbed9681fdd1aa7b3fd86dfb2c59b8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 5 Dec 2024 11:20:09 +0100 Subject: [PATCH] perf: improve string operations --- benchmark/compare-branches.js | 5 ++--- index.js | 16 ++++++++-------- lib/pretty-print.js | 2 +- lib/strategies/accept-version.js | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/benchmark/compare-branches.js b/benchmark/compare-branches.js index fd29b7d..1bda36b 100644 --- a/benchmark/compare-branches.js +++ b/benchmark/compare-branches.js @@ -47,14 +47,13 @@ async function executeCommandOnBranch (command, branch) { function parseBenchmarksStdout (text) { const results = [] - const lines = text.split('\n') - for (const line of lines) { + for (const line of text.split('\n')) { const match = /^(.+?)(\.*) x (.+) ops\/sec .*$/.exec(line) if (match !== null) { results.push({ name: match[1], alignedName: match[1] + match[2], - result: parseInt(match[3].split(',').join('')) + result: parseInt(match[3].replaceAll(',', '')) }) } } diff --git a/index.js b/index.js index eeee78f..e758124 100644 --- a/index.js +++ b/index.js @@ -184,8 +184,8 @@ Router.prototype._on = function _on (method, path, opts, handler, store) { if (!this.caseSensitive) { staticNodePath = staticNodePath.toLowerCase() } - staticNodePath = staticNodePath.split('::').join(':') - staticNodePath = staticNodePath.split('%').join('%25') + staticNodePath = staticNodePath.replaceAll('::', ':') + staticNodePath = staticNodePath.replaceAll('%', '%25') // add the static part of the route to the tree currentNode = currentNode.createStaticChild(staticNodePath) } @@ -240,8 +240,8 @@ Router.prototype._on = function _on (method, path, opts, handler, store) { let staticPart = pattern.slice(staticPartStartIndex, j) if (staticPart) { - staticPart = staticPart.split('::').join(':') - staticPart = staticPart.split('%').join('%25') + staticPart = staticPart.replaceAll('::', ':') + staticPart = staticPart.replaceAll('%', '%25') regexps.push(backtrack = escapeRegExp(staticPart)) } @@ -328,8 +328,8 @@ Router.prototype.findRoute = function findNode (method, path, constraints = {}) if (!this.caseSensitive) { staticNodePath = staticNodePath.toLowerCase() } - staticNodePath = staticNodePath.split('::').join(':') - staticNodePath = staticNodePath.split('%').join('%25') + staticNodePath = staticNodePath.replaceAll('::', ':') + staticNodePath = staticNodePath.replaceAll('%', '%25') // add the static part of the route to the tree currentNode = currentNode.getStaticChild(staticNodePath) if (currentNode === null) { @@ -387,8 +387,8 @@ Router.prototype.findRoute = function findNode (method, path, constraints = {}) let staticPart = pattern.slice(staticPartStartIndex, j) if (staticPart) { - staticPart = staticPart.split('::').join(':') - staticPart = staticPart.split('%').join('%25') + staticPart = staticPart.replaceAll('::', ':') + staticPart = staticPart.replaceAll('%', '%25') regexps.push(backtrack = escapeRegExp(staticPart)) } diff --git a/lib/pretty-print.js b/lib/pretty-print.js index 3d8d9dc..c5db18a 100644 --- a/lib/pretty-print.js +++ b/lib/pretty-print.js @@ -17,7 +17,7 @@ function printObjectTree (obj, parentPrefix = '') { const childPrefix = isLast ? ' ' : '│ ' const nodeData = value[treeDataSymbol] || '' - const prefixedNodeData = nodeData.split('\n').join('\n' + parentPrefix + childPrefix) + const prefixedNodeData = nodeData.replaceAll('\n', '\n' + parentPrefix + childPrefix) tree += parentPrefix + nodePrefix + key + prefixedNodeData + '\n' tree += printObjectTree(value, parentPrefix + childPrefix) diff --git a/lib/strategies/accept-version.js b/lib/strategies/accept-version.js index 3a5f785..91796d1 100644 --- a/lib/strategies/accept-version.js +++ b/lib/strategies/accept-version.js @@ -18,7 +18,7 @@ SemVerStore.prototype.set = function (version, store) { if (typeof version !== 'string') { throw new TypeError('Version should be a string') } - let [major, minor, patch] = version.split('.') + let [major, minor, patch] = version.split('.', 3) if (isNaN(major)) { throw new TypeError('Major version must be a numeric value')