Event loop Memes

Posts tagged with Event loop

Technically Horrifyingly Correct

Technically Horrifyingly Correct
The code creates a sorting algorithm that's technically O(n) but for all the wrong reasons. Instead of actually sorting the array, it's using setTimeout() with the array value as the delay time in milliseconds. The smallest numbers appear first in the console simply because their timeouts complete faster! It's like telling your friends you've invented a revolutionary sorting algorithm, but you're actually just making each number raise its hand after waiting for X milliseconds where X equals its own value. Pure chaotic genius. The browser's event loop is doing the sorting for free! Computational complexity professors are currently rolling in their graves (even the ones who aren't dead yet).

Timeout Sort: The Accidental Sorting Algorithm

Timeout Sort: The Accidental Sorting Algorithm
Behold the accidental genius of setTimeout sorting! The code loops through an array and logs each value using setTimeout with the value itself as the delay. Since JavaScript's event loop processes timeouts in order of expiration, smaller numbers appear first in the console. Congratulations! You've invented the world's most inefficient sorting algorithm with O(max(array)) time complexity. The array magically appears sorted in the console, not because of any actual sorting logic, but because the browser's event scheduler is doing all the work. Somewhere, a computer science professor just felt a disturbance in the force.