Skip to content

Commit 0b1f005

Browse files
perf(scripts): use printf -v to avoid subshell in json_escape loop
Replace code=$(printf ...) with printf -v code to assign the character code without spawning a subshell on every byte, reducing overhead for longer inputs.
1 parent 1b7ce3b commit 0b1f005

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

scripts/bash/common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ json_escape() {
179179
local i char code
180180
for (( i=0; i<${#s}; i++ )); do
181181
char="${s:$i:1}"
182-
code=$(printf '%d' "'$char" 2>/dev/null || echo 256)
182+
printf -v code '%d' "'$char" 2>/dev/null || code=256
183183
if (( code >= 1 && code <= 31 )); then
184184
printf '\\u%04x' "$code"
185185
else

0 commit comments

Comments
 (0)