Skip to content

Commit

Permalink
perf: improve string operations (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday authored Dec 18, 2024
1 parent 1e3fd64 commit 4a2e5a7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
5 changes: 2 additions & 3 deletions benchmark/compare-branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(',', ''))
})
}
}
Expand Down
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}

Expand Down
2 changes: 1 addition & 1 deletion lib/pretty-print.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/strategies/accept-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 4a2e5a7

Please sign in to comment.