I'm doing this fun coding challenge that I found at a meetup (doyouevendev.org)
What is the fastest way to generate a million clicks on an element? The coding challenge seems to be centred around the inspector, which I'm finding worthwhile.
My code (that i'm executing in chrome command line):
var item = document.getElementsByClassName("clicky-button pulse");
var item = item[0];
count = 0;
(function clickIt() {
count += 1
setInterval(function changeClicks() {
item.click();
}, 1);
if (count <= 50) {
clickIt();
};
})();
I suspect there's a better way... It actually seems to be slowing down...
The 'negative' while
loop should be slightly faster:
var i = 1000001;
while (--i) {
item.click();
}
Choose one: http://www.stoimen.com/blog/2012/01/24/javascript-performance-for-vs-while/
Javascript Performance: While vs For Loops
JavaScript is single-threaded so keep it simple:
for (var i = 0; i < 1000000; i++) {
item.click();
}
EDIT: Looks like @romanperekhrest's answer may be slightly faster since it's using a decrementing while loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With