Performance: Encoding and decoding URLs
This concept affects how quickly URLs are processed and safely transmitted, impacting network requests and page load speed.
Jump into concepts and practice - no test required
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 |
encodeURIComponent function do in Node.js?encodeURIComponentdecodeURIComponent is used to decode encoded parts of a URL.decodeURIComponent('https%3A%2F%2Fexample.com') correctly decodes the encoded string.const encoded = encodeURIComponent('Hello World!');
console.log(encoded);const url = 'https://example.com/search?query=Node.js'; const encodedUrl = encodeURIComponent(url); console.log(encodedUrl);
encodeURIComponent encodes all special characters, including ':' and '/', which breaks full URLs.encodeURI is designed to encode full URLs without breaking their structure.user@example.com. Which code snippet correctly encodes this parameter for use in a URL?encodeURIComponent to encode special characters like '@'.const param = encodeURIComponent('user@example.com'); correctly encodes '@' as '%40'.