From 0b3968c468a4fb3ce97fdf74cb619e7c0922d372 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sun, 10 Sep 2023 14:07:00 +0200 Subject: chore: speed up test name escapeing (#20439) --- cli/js/40_testing.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'cli/js') diff --git a/cli/js/40_testing.js b/cli/js/40_testing.js index 44b360a59..37eb6be8f 100644 --- a/cli/js/40_testing.js +++ b/cli/js/40_testing.js @@ -526,12 +526,27 @@ const ESCAPE_ASCII_CHARS = [ ["\v", "\\v"], ]; +/** + * @param {string} name + * @returns {string} + */ function escapeName(name) { - for (const [escape, replaceWith] of ESCAPE_ASCII_CHARS) { - name = StringPrototypeReplaceAll(name, escape, replaceWith); + // Check if we need to escape a character + for (let i = 0; i < name.length; i++) { + const ch = name.charCodeAt(i); + if (ch <= 13 && ch >= 8) { + // Slow path: We do need to escape it + for (const [escape, replaceWith] of ESCAPE_ASCII_CHARS) { + name = StringPrototypeReplaceAll(name, escape, replaceWith); + } + return name; + } } + + // We didn't need to escape anything, return original string return name; } + /** * @typedef {{ * id: number, -- cgit v1.2.3