Skip to content

Commit 4a2e5a7

Browse files
authored
perf: improve string operations (#382)
1 parent 1e3fd64 commit 4a2e5a7

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

benchmark/compare-branches.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ async function executeCommandOnBranch (command, branch) {
4747
function parseBenchmarksStdout (text) {
4848
const results = []
4949

50-
const lines = text.split('\n')
51-
for (const line of lines) {
50+
for (const line of text.split('\n')) {
5251
const match = /^(.+?)(\.*) x (.+) ops\/sec .*$/.exec(line)
5352
if (match !== null) {
5453
results.push({
5554
name: match[1],
5655
alignedName: match[1] + match[2],
57-
result: parseInt(match[3].split(',').join(''))
56+
result: parseInt(match[3].replaceAll(',', ''))
5857
})
5958
}
6059
}

index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
184184
if (!this.caseSensitive) {
185185
staticNodePath = staticNodePath.toLowerCase()
186186
}
187-
staticNodePath = staticNodePath.split('::').join(':')
188-
staticNodePath = staticNodePath.split('%').join('%25')
187+
staticNodePath = staticNodePath.replaceAll('::', ':')
188+
staticNodePath = staticNodePath.replaceAll('%', '%25')
189189
// add the static part of the route to the tree
190190
currentNode = currentNode.createStaticChild(staticNodePath)
191191
}
@@ -240,8 +240,8 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
240240

241241
let staticPart = pattern.slice(staticPartStartIndex, j)
242242
if (staticPart) {
243-
staticPart = staticPart.split('::').join(':')
244-
staticPart = staticPart.split('%').join('%25')
243+
staticPart = staticPart.replaceAll('::', ':')
244+
staticPart = staticPart.replaceAll('%', '%25')
245245
regexps.push(backtrack = escapeRegExp(staticPart))
246246
}
247247

@@ -328,8 +328,8 @@ Router.prototype.findRoute = function findNode (method, path, constraints = {})
328328
if (!this.caseSensitive) {
329329
staticNodePath = staticNodePath.toLowerCase()
330330
}
331-
staticNodePath = staticNodePath.split('::').join(':')
332-
staticNodePath = staticNodePath.split('%').join('%25')
331+
staticNodePath = staticNodePath.replaceAll('::', ':')
332+
staticNodePath = staticNodePath.replaceAll('%', '%25')
333333
// add the static part of the route to the tree
334334
currentNode = currentNode.getStaticChild(staticNodePath)
335335
if (currentNode === null) {
@@ -387,8 +387,8 @@ Router.prototype.findRoute = function findNode (method, path, constraints = {})
387387

388388
let staticPart = pattern.slice(staticPartStartIndex, j)
389389
if (staticPart) {
390-
staticPart = staticPart.split('::').join(':')
391-
staticPart = staticPart.split('%').join('%25')
390+
staticPart = staticPart.replaceAll('::', ':')
391+
staticPart = staticPart.replaceAll('%', '%25')
392392
regexps.push(backtrack = escapeRegExp(staticPart))
393393
}
394394

lib/pretty-print.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function printObjectTree (obj, parentPrefix = '') {
1717
const childPrefix = isLast ? ' ' : '│ '
1818

1919
const nodeData = value[treeDataSymbol] || ''
20-
const prefixedNodeData = nodeData.split('\n').join('\n' + parentPrefix + childPrefix)
20+
const prefixedNodeData = nodeData.replaceAll('\n', '\n' + parentPrefix + childPrefix)
2121

2222
tree += parentPrefix + nodePrefix + key + prefixedNodeData + '\n'
2323
tree += printObjectTree(value, parentPrefix + childPrefix)

lib/strategies/accept-version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SemVerStore.prototype.set = function (version, store) {
1818
if (typeof version !== 'string') {
1919
throw new TypeError('Version should be a string')
2020
}
21-
let [major, minor, patch] = version.split('.')
21+
let [major, minor, patch] = version.split('.', 3)
2222

2323
if (isNaN(major)) {
2424
throw new TypeError('Major version must be a numeric value')

0 commit comments

Comments
 (0)