Performance: REST API with JavaScript
MEDIUM IMPACT
This concept affects page load speed and interaction responsiveness by how API calls are made and handled in the browser.
fetch('https://example.com/wp-json/wp/v2/posts') .then(res => res.json()) .then(data => { const html = data.map(post => `<p>${post.title.rendered}</p>`).join(''); document.body.innerHTML += html; });
const response = fetch('https://example.com/wp-json/wp/v2/posts').then(res => res.json()).then(data => { for(let i=0; i<data.length; i++) { document.body.innerHTML += `<p>${data[i].title.rendered}</p>`; } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Updating DOM inside loop | Multiple insertions | N reflows for N items | High paint cost | [X] Bad |
| Batch DOM update with single insertion | Single insertion | 1 reflow | Low paint cost | [OK] Good |