Performance: Encoding and decoding URLs
MEDIUM IMPACT
This concept affects how quickly URLs are processed and safely transmitted, impacting network requests and page load speed.
const url = new URL('https://example.com/search'); url.searchParams.set('q', userInput); const safeUrl = url.toString();
const unsafeEncode = (str) => str.replace(/ /g, '%20').replace(/&/g, '%26'); const url = 'https://example.com/search?q=' + unsafeEncode(userInput);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual string replace for encoding | 0 | 0 | Low but error-prone | [X] Bad |
| Using URL and URLSearchParams APIs | 0 | 0 | Low and reliable | [OK] Good |
| decodeURIComponent without error handling | 0 | 0 | Blocks script on error | [X] Bad |
| decodeURIComponent with try-catch | 0 | 0 | Non-blocking, safe | [OK] Good |