Which of the following improves JavaScript performance?
A
Using global variables extensively
C
Using `eval()` frequently
D
Avoiding local variables
Analysis & Theory
Accessing the DOM is slow; reducing DOM manipulation improves performance.
What’s a better choice for performance when selecting DOM elements?
A
document.getElementById()
B
document.querySelectorAll('*')
Analysis & Theory
`getElementById()` is faster and more efficient for specific element lookups.
What is the effect of memory leaks in JavaScript?
C
App slows down or crashes over time
D
It improves garbage collection
Analysis & Theory
Memory leaks cause performance degradation as unused memory accumulates.
How can you reduce reflows and repaints in the browser?
A
Modify DOM styles one at a time
B
Batch DOM changes together
C
Use `alert()` after every change
D
Use inline styles for every element
Analysis & Theory
Batching style changes reduces layout thrashing and improves performance.
Which loop is generally faster for large iterations?
D
while accessing DOM each time
Analysis & Theory
Indexed `for` loops are typically faster than other iteration methods.
How does lazy loading help performance?
A
It loads all scripts immediately
B
It delays loading of non-critical resources
Analysis & Theory
Lazy loading delays loading of offscreen or non-critical elements to improve page speed.
Which is a better method for animation performance?
C
requestAnimationFrame()
D
loop with `while(true)`
Analysis & Theory
`requestAnimationFrame()` synchronizes with the browser's repaint cycle and is optimized for animations.
What should you do with large arrays or data sets?
A
Process them all at once
B
Use pagination or chunk processing
Analysis & Theory
Processing data in chunks or pages helps avoid UI freezes and improves responsiveness.
What is one way to avoid blocking the main thread?
A
Use synchronous functions
B
Run everything in one large function
C
Use Web Workers for heavy computations
Analysis & Theory
Web Workers allow running heavy code in background threads.
Which of the following is a good practice for loading JavaScript files efficiently?
A
Use `<script>` in the `<head>` without `defer`
B
Place scripts at the top of the page
C
Use `defer` or `async` in script tags
D
Load all JavaScript from inline HTML
Analysis & Theory
`defer` and `async` allow scripts to load without blocking page rendering.