Skip to content

Commit ffcb4c1

Browse files
authored
Improve escapeIdentifier performance on long strings by 47x
1 parent 9174783 commit ffcb4c1

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

packages/pg/lib/utils.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -173,35 +173,22 @@ const escapeIdentifier = function (str) {
173173
}
174174

175175
const escapeLiteral = function (str) {
176-
let hasBackslash = false
177-
let escaped = "'"
178-
179-
if (str == null) {
180-
return "''"
181-
}
182-
183176
if (typeof str !== 'string') {
184177
return "''"
185178
}
186-
187-
for (let i = 0; i < str.length; i++) {
188-
const c = str[i]
189-
if (c === "'") {
190-
escaped += c + c
191-
} else if (c === '\\') {
192-
escaped += c + c
193-
hasBackslash = true
194-
} else {
195-
escaped += c
196-
}
197-
}
198-
199-
escaped += "'"
200-
201-
if (hasBackslash === true) {
202-
escaped = ' E' + escaped
179+
let hasBackslash = false
180+
let escaped = str
181+
.replace(/\\/g, () => {
182+
hasBackslash = true
183+
return '\\\\'
184+
})
185+
.replace(/'/g, "''")
186+
187+
if (hasBackslash) {
188+
escaped = ` E'${escaped}'`
189+
} else {
190+
escaped = `'${escaped}'`
203191
}
204-
205192
return escaped
206193
}
207194

0 commit comments

Comments
 (0)