Performance: REST API with JavaScript
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 |
fetch('/wp-json/wp/v2/posts')
.then(res => res.json())
.then(posts => {
posts.forEach(post => console.log(post.title.rendered));
});async function getPages() {
const response = await fetch('wp-json/wp/v2/pages');
const data = await response.json;
console.log(data);
}Pass the test and this pattern moves unknown