Here is the interview question prompt, presented for reference.
Both debouncing and throttling are intended to enhance a website's performance. They accomplish this primarily by limiting the number of times a function can execute.
In the case of throttling, the exact definition is that it's a technique where a function can be executed only once in any given time interval-- regardless of how many times the user fires the event.
When is this useful? Consider the example of infinite scroll. Each time the user scrolls to the bottom of the page, we'll want to execute a function to grab the next list of resources to display. However, it's really easy for a user to scroll back up a bit and then try again, possibly several times, if the results don't load fast enough. If we throttle that call, we won't make unnecessary, expensive network calls.
Please use the following to test your code, comments in-line:
var assert = require('assert');
function throttle(func, waitTime) {
// fiill in this method
}
let time = 0;
function testThrottle(input) {
const calls = [];
time = 0;
function wrapper(arg) {
calls.push(`${arg}@${time}`);
}
const throttledFunc = throttle(wrapper, 3);
input.forEach((call) => {
const [arg, time] = call.split("@");
setTimeout(() => throttledFunc(arg), time);
});
return calls;
}
expect(testThrottle(["A@0", "B@2", "C@3"])).toEqual(["A@0", "C@3"]);
You can see the full challenge with visuals at this link.
Challenges • Asked over 2 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Implement Throttle (Main Thread).
Check these Tips to Get a Job After College With No Experience. No more doubts whether you’ll get the desired position. You’ll definitely succeed after reading the post.