Don’t pay much (if any) attention to this for now! Mark pointed out in the comments that a big part of the performance decrease is due to the unshift() function. Imma gunna retest this and update accordingly. Cheers Mark for letting me know!
Preamble: I was playing around with a simple test the other night to prove to myself that gamblers fallacy is, just that, a fallacy! It’s something I’ve known for a long time, though there is still always this part of my brain saying “nooooo! It’s way more likely to be heads after these 3 tails!”. Recently I have been reading “Thinking, fast and slow” by Danie Kahneman which when talking about errors of `fast` judgements really made me think that it’s worth making a big deal out of this to try to make better judgements!
This post is about the performance of arrays in javascript and my experience of being humiliated in these matters by a friend (@BMirey). I wrote a simple test which loops over, doing a million coin flips and has a set of functions that return their guess for the current flip. One of these knows the result of the last few flips which it stores and bets on heads if it’s seen more tails recently and vice-versa (if it’s seen equal amounts then it’s random). Real simple stuff. The shocker here is that it took well over an hour to do a million runs!
The guessing logic looked like this:
While this was running Mike was, rightly, highly suspicious about how long things were taking. He set to work on a python port of my ~70 line coffee file, which can be seen here His ran in 14 seconds providing the answers as expected also! But… but? Something was wrong, surely! Mikes python code was around 300 times faster than my coffeescript, which is 2 orders of anyones magnitude.
I had a look about and found this stack overflow question and decided that I should have a look at using typed arrays! My modified code looks like this:
The million iterations now runs in 2.7 seconds - much better! It’s worth noting that removing the divisions helps (just uses counts without division as in the first example), but it’s got nothing on the speedup from switching to a proper typed array.
The entire coffee file to prove to the gambler within that it’s a fallacy can be found here.
The lesson for me is that when doing something math heavy with arrays - use a proper typed array and consequently don’t do hash table lookups on every array access! I’ll do a proper analysis and a few graphs when I have my desktop in a few days!