Performance: Relative vs absolute URL resolution
This concept affects how URLs are resolved in web requests, impacting network request efficiency and caching behavior.
Jump into concepts and practice - no test required
const baseUrl = 'https://example.com/assets/'; const imageUrl = new URL('data.json', baseUrl).href; fetch(imageUrl).then(res => res.json());
const imageUrl = 'https://example.com/assets/data.json';
fetch(imageUrl).then(res => res.json());| Pattern | DNS Lookups | Cache Efficiency | Network Overhead | Verdict |
|---|---|---|---|---|
| Absolute URL | Multiple per resource | Lower due to domain changes | Higher due to repeated lookups | [X] Bad |
| Relative URL | Single or none | Higher due to consistent domain | Lower network overhead | [OK] Good |
const { URL } = require('url');
const base = 'https://example.com/folder/';
const relative = '../image.png';
const fullUrl = new URL(relative, base);
console.log(fullUrl.href);const { URL } = require('url');
const base = 'https://example.com/path';
const relative = 'file.txt';
const url = new URL(relative, base);
console.log(url.href);