MDL-82754 core: Improve treatStringsInContent() performance with arrays

Co-authored-by: JeanSotoriva <jeansotoriva@gmail.com>
This commit is contained in:
meirzamoodle 2024-09-23 12:11:13 +07:00
parent 0888a6d324
commit 58b9be52d5
3 changed files with 33 additions and 12 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -477,7 +477,7 @@ export default class Renderer {
// Placeholders are in the for [[_sX]] or [[_cX]] where X is the string index.
const stringPattern = /(?<placeholder>\[\[_(?<stringType>[cs])(?<stringIndex>\d+)\]\])/g;
// A helpre to fetch the string for a given placeholder.
// A helper to fetch the string for a given placeholder.
const getUpdatedString = ({placeholder, stringType, stringIndex}) => {
if (stringMap.has(placeholder)) {
return stringMap.get(placeholder);
@ -493,20 +493,41 @@ export default class Renderer {
}
Log.debug(`Could not find string for pattern ${placeholder}`);
return '';
return ''; // Fallback if no match is found.
};
// Find all placeholders in the content and replace them with their respective strings.
let match;
while ((match = stringPattern.exec(content)) !== null) {
let updatedContent = content.slice(0, match.index);
updatedContent += getUpdatedString(match.groups);
updatedContent += content.slice(match.index + match.groups.placeholder.length);
let updatedContent = content; // Start with the original content.
let placeholderFound = true; // Flag to track if we are still finding placeholders.
content = updatedContent;
// Continue looping until no more placeholders are found in the updated content.
while (placeholderFound) {
let match;
let result = [];
let lastIndex = 0;
placeholderFound = false; // Assume no placeholders are found.
// Find all placeholders in the content and replace them with their respective strings.
while ((match = stringPattern.exec(updatedContent)) !== null) {
placeholderFound = true; // A placeholder was found, so continue looping.
// Add the content before the matched placeholder.
result.push(updatedContent.slice(lastIndex, match.index));
// Add the updated string for the placeholder.
result.push(getUpdatedString(match.groups));
// Update lastIndex to move past the current match.
lastIndex = match.index + match[0].length;
}
// Add the remaining part of the content after the last match.
result.push(updatedContent.slice(lastIndex));
// Join the parts of the result array into the updated content.
updatedContent = result.join('');
}
return content;
return updatedContent; // Return the fully updated content after all loops.
}
/**