Blocking Code

noun

say it: “BLOK-ing kohd” /ˈblɑkɪŋ koʊd/

Code that keeps JavaScript busy for so long that nothing else can run, so the page freezes until it finishes.

What is Blocking Code in JavaScript?

Blocking Code — Code that keeps JavaScript busy until it finishes, such as a long loop. JavaScript does one thing at a time, so while it runs, nothing else can: clicks wait, timers wait, and a page freezes.

const start = Date.now();
while (Date.now() - start < 3000) {
  // busy for 3 seconds: a page would freeze here
}
console.log('Done waiting'); // printed after 3 seconds

In a sentence

  • “I moved the slow sorting out of the click listener, because blocking code there made every click feel stuck.”
  • “The loop that counted to a billion was blocking code, so the page froze and the buttons stopped answering.”
  • “I replaced the blocking code that waited in a loop with a timer, and the page stopped freezing.”
  • “A loop that counted to ten billion was blocking code: no button on the page worked until it was done.”

Level: Apprentice · More JavaScript terms at this level

Updated