Skip to content

Commit 966fa88

Browse files
Optimize performance by replacing array methods with manual loops
Co-authored-by: joaquim.verges <[email protected]>
1 parent 95735a2 commit 966fa88

File tree

1 file changed

+99
-22
lines changed

1 file changed

+99
-22
lines changed

apps/dashboard/src/app/(app)/(dashboard)/published-contract/[publisher]/[contract_id]/utils/publishedContractOGImageTemplate.tsx

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,73 @@ const VersionIcon: React.FC = () => (
110110
const MAX_LENGTH = 190;
111111

112112
function descriptionShortener(description: string) {
113-
let words = [];
113+
const words: string[] = [];
114+
let wordsCount = 0;
114115
let currentLength = 0;
115116
let shortened = false;
116-
for (const word of description.split(" ")) {
117+
118+
// Replace split with manual string parsing
119+
const descriptionWords: string[] = [];
120+
let currentWord = "";
121+
for (const char of description) {
122+
if (char === " ") {
123+
if (currentWord) {
124+
descriptionWords[descriptionWords.length] = currentWord;
125+
currentWord = "";
126+
}
127+
} else {
128+
currentWord += char;
129+
}
130+
}
131+
if (currentWord) {
132+
descriptionWords[descriptionWords.length] = currentWord;
133+
}
134+
135+
for (const word of descriptionWords) {
117136
// +1 for the space
118137
if (currentLength + word.length + 1 > MAX_LENGTH) {
119138
shortened = true;
120139
break;
121140
}
122-
words.push(word);
141+
words[wordsCount] = word;
142+
wordsCount++;
123143
currentLength += word.length + 1;
124144
}
125-
const lastWord = words[words.length - 1];
145+
146+
// Replace words[words.length - 1] with manual indexing
147+
const lastWord = wordsCount > 0 ? words[wordsCount - 1] : undefined;
126148
if (lastWord && lastWord.length < 4) {
127-
words = words.slice(0, -1);
149+
wordsCount--;
128150
}
129-
if (words[words.length - 1]?.endsWith(".")) {
130-
return words.join(" ");
151+
152+
// Replace words[words.length - 1] with manual indexing
153+
const finalLastWord = wordsCount > 0 ? words[wordsCount - 1] : undefined;
154+
if (finalLastWord?.endsWith(".")) {
155+
// Replace join with manual string building
156+
let result = "";
157+
for (let i = 0; i < wordsCount; i++) {
158+
if (i > 0) result += " ";
159+
result += words[i];
160+
}
161+
return result;
131162
}
132163
if (!shortened) {
133-
return words.join(" ");
164+
// Replace join with manual string building
165+
let result = "";
166+
for (let i = 0; i < wordsCount; i++) {
167+
if (i > 0) result += " ";
168+
result += words[i];
169+
}
170+
return result;
171+
}
172+
173+
// Replace join with manual string building
174+
let result = "";
175+
for (let i = 0; i < wordsCount; i++) {
176+
if (i > 0) result += " ";
177+
result += words[i];
134178
}
135-
return `${words.join(" ")} ...`;
179+
return `${result} ...`;
136180
}
137181

138182
export async function publishedContractOGImageTemplate(params: {
@@ -293,16 +337,27 @@ export async function publishedContractOGImageTemplate(params: {
293337
<PackageIcon />
294338
<span tw="ml-2">
295339
{Array.isArray(params.extension)
296-
? categorizeExtensions(params.extension).map(
297-
([ext, count]) => (
298-
<span key={ext} tw="flex flex-row items-center mr-3">
299-
{ext}
300-
<span tw="text-black px-3 h-auto font-bold m-1 rounded-full text-sm bg-white opacity-90">
301-
<span tw="m-auto">{count}</span>
340+
? (() => {
341+
const categorizedExtensions = categorizeExtensions(
342+
params.extension,
343+
);
344+
const extensionElements: React.ReactNode[] = [];
345+
for (const categoryData of categorizedExtensions) {
346+
const [ext, count] = categoryData;
347+
extensionElements[extensionElements.length] = (
348+
<span
349+
key={ext}
350+
tw="flex flex-row items-center mr-3"
351+
>
352+
{ext}
353+
<span tw="text-black px-3 h-auto font-bold m-1 rounded-full text-sm bg-white opacity-90">
354+
<span tw="m-auto">{count}</span>
355+
</span>
302356
</span>
303-
</span>
304-
),
305-
)
357+
);
358+
}
359+
return extensionElements;
360+
})()
306361
: params.extension}
307362
</span>
308363
</li>
@@ -312,7 +367,18 @@ export async function publishedContractOGImageTemplate(params: {
312367
<FileTextIcon />
313368
<span tw="ml-2">
314369
{Array.isArray(params.license)
315-
? params.license.join(", ")
370+
? (() => {
371+
let licenseString = "";
372+
let isFirst = true;
373+
for (const license of params.license) {
374+
if (!isFirst) {
375+
licenseString += ", ";
376+
}
377+
licenseString += license;
378+
isFirst = false;
379+
}
380+
return licenseString;
381+
})()
316382
: params.license}
317383
</span>
318384
</li>
@@ -384,7 +450,8 @@ function categorizeExtensions(extensions: string[]) {
384450
Other: 0,
385451
};
386452

387-
extensions.forEach((extension) => {
453+
// Replace forEach with for...of loop
454+
for (const extension of extensions) {
388455
if (extension.startsWith("ERC721")) {
389456
categoriesWithCount.ERC721 += 1;
390457
} else if (extension.startsWith("ERC1155")) {
@@ -394,6 +461,16 @@ function categorizeExtensions(extensions: string[]) {
394461
} else {
395462
categoriesWithCount.Other += 1;
396463
}
397-
});
398-
return Object.entries(categoriesWithCount).filter(([, count]) => count > 0);
464+
}
465+
466+
// Replace filter with for...of loop
467+
const result: Array<[string, number]> = [];
468+
const entries = Object.entries(categoriesWithCount);
469+
for (const entry of entries) {
470+
const [category, count] = entry;
471+
if (count > 0) {
472+
result[result.length] = [category, count];
473+
}
474+
}
475+
return result;
399476
}

0 commit comments

Comments
 (0)