0
0
Node.jsframework~8 mins

Relative vs absolute URL resolution in Node.js - Performance Comparison

Choose your learning style9 modes available
Performance: Relative vs absolute URL resolution
MEDIUM IMPACT
This concept affects how URLs are resolved in web requests, impacting network request efficiency and caching behavior.
Resolving URLs for fetching resources in a Node.js web app
Node.js
const baseUrl = 'https://example.com/assets/';
const imageUrl = new URL('data.json', baseUrl).href;
fetch(imageUrl).then(res => res.json());
Relative URLs reuse the current domain context, reducing DNS lookups and improving cache hits.
📈 Performance GainSaves DNS lookup time, reducing load time by ~50-100ms per resource
Resolving URLs for fetching resources in a Node.js web app
Node.js
const imageUrl = 'https://example.com/assets/data.json';
fetch(imageUrl).then(res => res.json());
Using absolute URLs causes repeated DNS lookups and prevents efficient caching when the base URL is known.
📉 Performance CostAdds extra DNS lookup per request, increasing load time by ~50-100ms per resource
Performance Comparison
PatternDNS LookupsCache EfficiencyNetwork OverheadVerdict
Absolute URLMultiple per resourceLower due to domain changesHigher due to repeated lookups[X] Bad
Relative URLSingle or noneHigher due to consistent domainLower network overhead[OK] Good
Rendering Pipeline
URL resolution happens before network requests; absolute URLs require full domain resolution, while relative URLs resolve locally based on the current context.
Network Request Preparation
DNS Lookup
Cache Lookup
⚠️ BottleneckDNS Lookup stage is most expensive when using absolute URLs repeatedly.
Core Web Vital Affected
LCP
This concept affects how URLs are resolved in web requests, impacting network request efficiency and caching behavior.
Optimization Tips
1Use relative URLs for resources on the same domain to reduce DNS lookups.
2Avoid absolute URLs unless fetching from a different domain or CDN.
3Consistent URL resolution improves caching and reduces network overhead.
Performance Quiz - 3 Questions
Test your performance knowledge
Which URL type reduces DNS lookups when fetching multiple resources from the same domain?
AAbsolute URL
BRelative URL
CData URL
DProtocol-relative URL
DevTools: Network
How to check: Open DevTools > Network tab, reload page, and observe DNS lookup timings and request URLs.
What to look for: Look for repeated DNS lookups and longer request times for absolute URLs versus faster requests for relative URLs.