From the course: Vanilla JavaScript: Web Performance Optimization APIs
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Efficient script yielding - JavaScript Tutorial
From the course: Vanilla JavaScript: Web Performance Optimization APIs
Efficient script yielding
- [Instructor] The final API available in modern browsers regarding executing code is efficient script yielding. It's a way to yield control to the browser before actually running our code. In this case, it's for normal to high-priority code that we want to execute. So it's not low priority. We want to execute that code as soon as possible, but we want to do that after the browser has rendered any changes on the screen. Typically, we were using the setTimeout zero to do this, I've been doing this a lot in web apps, since probably 1998, but the problem with setTimeout zero is that it always has some time that is being used for other purposes, so it's not going to execute your code as soon as possible when you are using setTimeout. So, instead of doing that, we can access the new API that is really simple, it's just setImmediate. We send a callback, and the code that we are executing there will be executed exactly after the browser has finished parsing and rendering any pending changes…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.