I’ve been itching to write some JavaScript, so a few days ago I threw together a Checkers game that uses only HTML5 and JS. It’s got a simple AI that terminates after three seconds. The faster your browser can run JavaScript, the smarter the AI will be.
For example, here is Firefox 4 Beta (blue) versus Firefox 3 (red) – it happens that Firefox 4 is roughly 10X faster at this program, and soundly defeated its predecessor:
The algorithm for the AI is UCT, a form of Monte-Carlo Tree Search. The idea is to estimate the likelihood of winning from a position by simulating hundreds or thousands of random games. UCT helps prune the search space by quickly eliminating bad positions.
While writing the source for this, I tried to turn off the part of my brain that said, “Hey! I know Firefox version x.y.z might be slow or fast at feature X.” That turned out to be okay, but it was harder to avoid general knowledge about JavaScript engines. I ended up with three variations:
- Fast Checkers, which has manual inlining, manual strength reduction, hardcoded constants, and other gross stuff. Pieces and positions are represented via packed integers.
- Slow Checkers, which removes manual inlining, strength reduction, and baked-in constants. Here, the additional overhead is extra memory accesses and function calls.
- OO Checkers, which is the same as “slow”, except represents pieces as objects instead of packed integers. Here, an additional overhead is object allocation.
Performance seems uniform across most browsers. Below I’ve recorded the number of games each browser could simulate per second. Higher is better. Note – this chart is totally unscientific, and random simulations are affected by the behavior of Math.random().
Fast Checkers | Slow Checkers | OO Checkers | |
---|---|---|---|
Firefox 4 Beta | 14035 | 9018 | 9100 |
IE9 PP6 | 14579 | 8234 | 8067 |
Opera 11 Alpha | 13165 | 8178 | 8749 |
Safari 5 | 12442 | 8045 | 8700 |
Chrome 9 Canary | 4160 | 2060 | 2343 |
And – because why not – I ran them on my Droid 2 as well.
Fast Checkers | Slow Checkers | OO Checkers | |
---|---|---|---|
Fennec 2b3pre | 338 | 170 | 220 |
Android Browser | 185 | 93 | 114 |
Opera Mobile | 166 | 112 | 126 |
Since I’m pretty bad at web development, and don’t write JavaScript (sans test-cases) nearly as much as I should, this was an amusing experience. I kept making some really dumb mistakes, one repeatedly:
Game.prototype.player = function () { return this.board.player; } ... var player = game.player; if (player == x) { ...
And wondering why “player” showed as a function in the developer console. I probably should have used ES5 getters. A few other language features would have made the experience a little nicer – but nothing so much as real modules. I tried to emulate good encapsulation with closures, but it’s just not the same. And it doesn’t seem like any engine is smart enough yet to propagate constants through closures (which is one difference between the “fast” and “slow” variants).
Using developer tools for the first time was also an interesting experience. Firefox 4 and Chrome can debug code with a reasonable slow-down, but IE9 became over 100X slower; presumably it cannot debug with the JIT yet. I used Firebug until I needed single-stepping (we didn’t have that working in JägerMonkey for Beta 7), and then bounced over to Chrome – both proved really invaluable. I think my days of calling alert() are over.
Anyway, it was fun writing this, and I’m glad that I can write something computationally expensive and have it be fast everywhere. If and when time permits I may try a more stimulating game like Go.
Excellent way of describing, and fastidious post to obtain facts on the
topic of my presentation subject matter, which i am going to convey in university.
Hello, after reading this amazing piece of writing i am as well glad to share my knowledge here with colleagues.
Why visitors still use to read news papers when in this
technological world all is available on net?
It’s awesome to visit this web site and reading the views of all mates on the topic of this article, while I am also zealous of getting familiarity.
Helpful info. Fortunate me I discovered your web site by chance, and I am
shocked why this twist of fate didn’t happened earlier! I bookmarked it.
Hi there, all is going perfectly here and ofcourse every one is sharing facts, that’s truly fine, keep up writing.
you’re truly a excellent webmaster. The site loading speed is amazing.
It kind of feels that you are doing any distinctive trick.
Furthermore, The contents are masterwork. you
have done a fantastic activity in this topic!
I was wondering if you ever thought of changing the
layout of your blog? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content
so people could connect with it better. Youve got an awful lot of
text for only having 1 or 2 pictures. Maybe you could space it
out better?
What’s up mates, pleasant paragraph and nice arguments commented at this place,
I am in fact enjoying by these.
Thanks , I have recently been searching for information approximately this topic for a while and yours is the best I’ve discovered till now.
However, what about the bottom line? Are you sure concerning
the supply?
Have you ever considered creating an ebook or guest authoring on other sites?
I have a blog based on the same ideas you
discuss and would love to have you share some stories/information. I
know my viewers would enjoy your work. If you are even remotely interested, feel free
to shoot me an e mail.
Howdy!
You Need Leads, Sales, Conversions, Traffic for bailopan.net ? Will Findet…
I WILL SEND 5 MILLION MESSAGES VIA WEBSITE CONTACT FORM
Don’t believe me? Since you’re reading this message then you’re living proof that contact form advertising works!
We can send your ad to people via their Website Contact Form.
IF YOU ARE INTERESTED, Contact us => [email protected]
Regards,
Nevile
Hello Dear, are you genuinely visiting this website daily,
if so then you will definitely get good know-how.
Fine way of telling, and pleasant article to obtain facts on the topic of my presentation subject, which i am going to deliver in school.
Excellent post but I was wondering if you could write a litte more on this subject?
I’d be very grateful if you could elaborate a little bit more.
Cheers!
핸드폰바둑이이용방법 so funny 사설바둑이추천사이트 http://www.mofg3.com
Superb, what a web site it is! This webpage gives helpful data to us, keep it up.
Because the admin of this web page is working, no doubt very shortly it will be
famous, due to its feature contents.
14 Savvy Ways to Spend Leftover 안전카지노 Budget http://www.ssuncasino.com
Everything is very open with a clear clarification of the challenges.
It was really informative. Your site is extremely helpful.
Thank you for sharing!
Now I am going away to do my breakfast, when having my breakfast coming yet again to read further news.
I quite like reading through an article that
can make people think. Also, thanks for allowing for me
to comment!
Thank you for the good writeup. It in truth was once a leisure account it.
Glance complex to far introduced agreeable from you!
However, how could we be in contact?
핸드폰바둑이 so wow 핸드폰바둑이이용방법 http://www.mofg3.com
The No. 1 Question Everyone Working in 실제딜러카지노 Should Know How to Answer http://www.ssuncasino.com
I am in fact glad to read this webpage posts
which carries tons of helpful information, thanks for providing
these statistics.
15 People You Oughta Know in the 바카라추천 Industry http://www.ssuncasino.com
바둑이추천인 so funny 안전한바둑이 http://www.mofg3.com
바둑이총판 so crazy 해적게임 http://www.mofg3.com
루비게임 so wow 현금바둑이 http://www.mofg3.com
337게임 so crazy 바둑이사이트 http://www.mofg3.com
Hi there every one, here every person is sharing such know-how, therefore it’s pleasant to read this
webpage, and I used to go to see this webpage
all the time.
Hi colleagues, nice paragraph and good arguments commented here, I am really enjoying by these.
My brother recommended I might like this website.
He used to be totally right. This submit truly made my day.
You cann’t consider just how so much time I had spent for this
info! Thank you!
골목게임 so funny 해적게임 http://www.mofg3.com
클로버게임 so wow 바닐라게임 http://www.mofg3.com
Hi there
I will Find Leads that Buy From You
I will Promote Your Business In Any Country To Any Niche.
==> https://is.gd/lJ4yuZ
Regards
funny 바카라사이트 오케이카지노사이트주소 개츠비카지노쿠폰
whoah this weblog is great i like studying your posts.
Stay up the good work! You already know, many people are searching round for
this information, you can help them greatly.
For most up-to-date news you have to visit the web and on the web I found this site as a most excellent web site for
hottest updates.
Undeniably imagine that that you stated. Your favourite justification seemed to be on the net the simplest factor
to take into accout of. I say to you, I definitely get irked at the same time as other folks consider concerns that
they plainly don’t know about. You managed to hit the nail upon the top and
also defined out the whole thing without having side-effects , folks can take a signal.
Will likely be back to get more. Thank you
Awesome issues here. I’m very satisfied to peer your post.
Thank you so much and I’m taking a look ahead to touch you.
Will you kindly drop me a mail?
Thank you for sharing your info. I really appreciate your
efforts and I am waiting for your next post thanks once again.
Where Will 무료카지노사이트 Be 1 Year From Now? http://www.ssuncasino.com
7 Simple Secrets to Totally Rocking Your 마이다스게임 http://www.ssuncasino.com
5 Tools Everyone in the 리얼카지노 Industry Should Be Using http://www.ssuncasino.com
A Productive Rant About 폰카지노사이트 http://www.ssuncasino.com
10 Signs You Should Invest in 모바일바카라사이트 http://www.ssuncasino.com
The Top Reasons People Succeed in the 오리엔탈카지노 Industry http://www.ssuncasino.com
The 13 Best Pinterest Boards for Learning About 카지노게임 http://www.ssuncasino.com