r/programming • u/alecdo • Dec 20 '10
A Christmas Tree in 1k of Javascript
http://www.romancortes.com/blog/how-i-did-the-1kb-christmas-tree/119
23
u/different2une Dec 20 '10
I once read (and executed) a tiny piece of obfuscated C code that when executed printed out "Twelve days of Christmas" ... I was awed!
edit: found it!
5
u/RedHaze Dec 21 '10
Great read! However, even though I read through the entire article I still couldn't fully grasp how this was done. I'm still throughly impressed though.
2
u/dmhouse Dec 21 '10
Is http://research.microsoft.com/en-us/um/people/tball/papers/XmasGift/final.html not clear enough? Which part don't you understand?
→ More replies (1)
42
u/ImTheGuyWithTheGun Dec 20 '10 edited Dec 20 '10
Amazing how much smoother this runs in Chrome compared to Firefox. And IE8 - forget about it.
EDIT: Safari 5 seems even worse than Firefox. My system is a core-i5 w/ 6GB on Win7 - should be plenty of horsepower.
21
u/rickmatt Dec 20 '10
I loaded it in Firefox 3.6.13 (on a 64 bit quad core desktop with 6G RAM running Windows 7 Ultimate) and my CPU usage went from 1% to 25%. Closed it and usage dropped to 1% again. Loaded it in Chrome and there was no appreciable increase in usage. WTF?
3
u/whoisrich Dec 20 '10
For anyone wanting to try FireFox 4 there is a portable edition here:
http://portableapps.com/apps/internet/firefox_portable/test
The Reddit comments load so much faster compared to Firefox 3.
8
Dec 20 '10
I don't know what could cause that but i'm suspecting that hardware acceleration has a lot to do with it. It seems that javascript is one of the things that is fully output with software only. If there was hardware acceleration then it would be blazing fast.
It's like trying to render out a 3d game with software (ie. CPU only). It doesn't matter what computer you use, it's going to get crushed trying to spit out those polygons. But on hardware acceleration it's all offloaded onto the GPU which is specially designed to handle just such things, which is how we enjoy things like that these days.
→ More replies (9)15
u/ascendant512 Dec 21 '10
I was forced to register because of this thread and some comments in it. The Chrome fanboys here are quite ridiculous.
Chrome does not have hardware-accelerated javascript. Hardware-accelerated javascript does not exist. You are mistaking it with WebGL which is present in Safari, Firefox (3 and 4) and Chrome. WebGL has nothing to do with javascript, it is a hardware accelerated <canvas> element (HTML5). Chrome has barely any more hardware acceleration than Microsoft Powerpoint.
Furthermore, the Christmas tree is not rendered with polygons, it is rendered with sprites. It says so right on the front page.
→ More replies (28)→ More replies (1)2
u/wefarrell Dec 21 '10
For js, chrome is several orders of magnitude faster than ff.
The ff beta is just as fast as chrome though.
4
u/abadidea Dec 21 '10
iirc the Mozilla Javascript team has said they're on track for catching up to Chrome. Chrome's JS has done to FF what Firefox did to the IE interface team several years ago: embarrass them into trying harder.
15
9
u/causticmango Dec 21 '10
Must be specific to Windows. On OS X, Safari is noticeably smoother (for this demo, at least).
2
Dec 21 '10
Not surprising, Safari probably uses HW acceleration for
canvason OS X, but not on Windows.7
7
u/krische Dec 20 '10
It was about the same speed wise for me in Firefox 4 beta 7 and Opera 11 as it was in Chrome.
6
u/ImTheGuyWithTheGun Dec 20 '10
I'm using FF 3.6.9. I know FF 4 supposedly has a much faster JS-engine -- good to hear that it's true. But in 3.6, it runs at a fraction of the framerate compared to chrome.
→ More replies (2)→ More replies (1)5
u/longmover79 Dec 20 '10
Smoothest/Fastest award goes to Safari followed by Chrome then FF a significant last (on mine at least)
3
u/ImTheGuyWithTheGun Dec 20 '10
Safari is a slide-show for me (dual core on Win7). Firefox is better, but still jittery. Chrome is silky smooth.
→ More replies (1)6
u/cruzin Dec 20 '10
Safari and Chrome look the same for me: smooth, no jitters. I'm using OS X, not Win7 though.
→ More replies (1)2
2
Dec 21 '10
Interesting. Safari 5 under OS X 10.6 is noticeably smoother than either Firefox or Chrome. And I have a pretty slow machine (2.16 GHz Core 1 Duo, Radeon X1600).
2
1
u/FractalP Dec 20 '10
Latest Firefox beta on a MacBook runs rather less smooth than it does on Opera 11 or Chrome. In fact, I'd say Opera runs slightly better than Chrome, although it isn't quite up to date.
1
u/rmart Dec 21 '10
That's weird, it works just fine in Safari on my Mac. Then again, I've got an i7 CPU so maybe it's just a case of Safari running it well when there's enough horsepower.
7
Dec 20 '10 edited Oct 02 '26
[removed] — view removed comment
12
u/mrkite77 Dec 20 '10
chrome has incredible javascript performance. I made a 68k emulator in javascript that had no problem running at 1mhz on chrome.
→ More replies (2)6
u/feureau Dec 21 '10
I made a 68k emulator in javascript
... whoat?!
68k emulator in javascript
@_@
68k emulator in javascript
WANT!!!
→ More replies (3)1
u/ToraxHunt Dec 20 '10
Each browser has stronger areas where they render faster. Chrome just happens to be good at this kind of thing. There are areas where each IE8-IE9 and Firefox are faster.
10
u/pkkid Dec 20 '10
Could you be a bit more specific please?
(curious minds want to know)
→ More replies (17)
26
12
10
u/jtreminio Dec 20 '10
Does look awesome.
Here's a fun trick for all: You can make that wireframe tree spin either left or right, using naught but your mind!
1
1
u/protonfish Dec 21 '10 edited Dec 21 '10
I don't think so. But I did stare at it for a while turning my head so touché.
To make it actually spin backward you could change the line D+=.02 to D = D -.02
Edit: Whoa, that also makes the snow fall up...
1
u/UsernameTaken123 Dec 21 '10
Ya that's what I noticed too, I thought it only worked with the ballet girl.
10
9
u/krische Dec 20 '10
Wow, it looks amazing. I can't believe it was done with only 1,000 characters.
37
2
8
13
u/abeliangrape Dec 20 '10
Can some explain to me how using cos(angle+11) for sin(angle) saves space?
32
u/cnk Dec 20 '10 edited Dec 20 '10
Look at the code:
M=Math T=M.sinThen he uses that trick for "cos" twice in the code:
T(R-11) //7 charactersThe other way to write that is: M.cos(R) //8 characters
Assigning cos to a variable is 7 additional chars (C=M.cos), using it that way is C(a) 4 chars, he uses cos twice in the code: that would be 7+4+4 total: 15. Using T(a-11) twice is 14 chars.
Also: assigning to a variable uses an additional semicolon which isn't necessary using the sin approximation
15
u/majoris Dec 20 '10 edited Dec 20 '10
Paraphrasing his code, here is how it looks with and without the trick:
M=Math;T=M.sin;T(a);T(a);T(a);T(a);T(a);T(a-11);T(a-11) M=Math;T=M.sin;S=M.cos;T(a);T(a);T(a);T(a);T(a);S(a);S(a)edit: who gave me upvotes without checking my solution!
6
1
4
u/Douglas77 Dec 21 '10
11 = 3.5 * PI
(with an error of 0.0044, that's 1/4 degree)
EDIT: Misunderstood your question.
5
4
u/Oddish Dec 21 '10
Although not as elite, I prefer this one:
8
u/InsideDown Dec 21 '10
Hey, that's mine! Never thought something I did would be randomly posted on reddit. Glad you liked it. (Nowhere near as impressive as Roman's though).
4
u/abadidea Dec 21 '10
I'm a dummy and thought that the static image was the result. "What are people talking about? This thing renders instantly..."
1
10
u/ercax Dec 20 '10
I didn't know that I could boil tea on my cpu.
24
u/skelooth Dec 20 '10
You mean you aren't running a tea cooled rig? It's been all the rage since the crumpet towers came out.
3
u/abadidea Dec 21 '10
Clearly, you don't play Dwarf Fortress.
(And people think that graphics are what's driving the hardware market.)
1
u/ercax Dec 21 '10
Dwarf Fortress? No! Just guessing from what I read; if I start playing that thing I will have no life. It looks too good.
2
u/abadidea Dec 21 '10
I am a fierce advocate and evangelist for Dwarf Fortress. It is literally the only reason I'll be looking for more than 1.6ghz on my next computer. It is a masterwork piece of simulation.
But I do too have a life. Because my fiance and my best friend play it too.
2
1
6
u/10goto10 Dec 20 '10
I love stuff like this. The fact that all of this is contained in 1K really blows my mind. Reminds me of being amazed of what the guys in the demo scene used to pull off.
7
Dec 20 '10
They're still going strong.
Here for your delight is 3.5 minutes of terraformed environments, music, light effects synced to music... in 4k.
→ More replies (2)6
u/10goto10 Dec 20 '10
I know, I visit several demoscene parties a year. I didn't want to imply there's nothing happening anymore, but modern PC demos just don't have the same appeal to me. Maybe because I lost oversight on what the limits really are that the coders have to work with. On the C64 I knew the limits of the machine by heart, which made it easy to recognize a spectacular effect.
I guess Javascript and the "browser environment" is just closer to home for me, seeing it is my daily work environment. That's why I like it so much!
3
19
u/naich Dec 20 '10 edited Dec 20 '10
http://js1k.com/2010-xmas/demo/856 - The actual demo
27
Dec 20 '10 edited Dec 20 '10
edit: ok now my comment looks stupid. to clarify: naich asked for a mirror...
2
1
u/allholy1 Dec 20 '10
huh?
15
Dec 20 '10
when i posted that comment, the comment of naich above asked for a mirror because the site linked to by the title was slow at that time. I provided that link, then he edited his comment to what it is now.
→ More replies (2)2
Dec 20 '10
My puter's CPU is screaming when viewing that page.
5
u/666pool Dec 20 '10
Mine was too in Firefox. Do you have chrome installed? It ran much smoother for me.
1
u/IllBeBack Dec 20 '10
I have both installed, and man, Chrome's Javascript execution is far faster than Firefox's. And I'm using the latest beta of Firefox 4.
1
Dec 21 '10
I haven't installed chrome, but I probably should try it out.
1
u/666pool Dec 21 '10
A lot of people are switching, but I still prefer Firefox. I haven't found a replacement for ad block plus that I like for Chrome :(
1
2
2
2
u/pkkid Dec 20 '10
I would love to see a version of this source thats not all obfuscated.
→ More replies (2)
2
u/Jackofallnutz Dec 20 '10
Wow, that is awesome! Glad I'm learning Javascript right now, this just adds to the excitement.
2
2
u/LieutenantClone Dec 21 '10
That is absolutely beautiful. You really don't get the full effect without running it and seeing how well the 3D part works. The 2D image does not do it justice whatsoever.
2
2
2
3
2
u/ConstipatedAsshole Dec 20 '10
Is it changing directions by itself? or is that some optical illusion.
6
Dec 20 '10
hmm, mine doesn't change...
don't think its an optical illusion, because the direction of movement and front/backside are clearly visible from the christmas bulbs.
3
u/hobophobe Dec 20 '10
It is an optical illusion. One of the interesting things about that form (assuming you mean the GIF in the article, not the tree itself) of it is that individual branches can appear to move in opposite directions simultaneously, because the whole isn't a solid form.
1
u/zitler Dec 20 '10
Aaauughhh why did I not pay attention in math class! I could be shitting diamond christmas trees made in JavaScript by now...
1
Dec 20 '10
Amazing stuff.
Just finished my first year of college so I haven't hit my CS classes; everything I see looks so damn intimidating.
0
u/Haxepoo Dec 21 '10
You will probably not see code like this in a CS class. You don't learn how to program/code in CS.
1
Dec 21 '10
Really? I was under the impression that would be at least part of it. Does this mean I should be trying to pick up a language on my own now?
1
1
1
u/a1phanumeric Dec 20 '10
Nice, except it took me about 20seconds to go back from the demo page. OSX, Firefox with Firebug (disabled though...) and the web developer toolbar.
1
u/Narcolepzzzzzzzzzzzz Dec 20 '10
Opera Mobile 10.1 on android can't render it but Dolphin HD can. Slow but it works.
1
u/causticmango Dec 21 '10
Sweet.
Odd observation; it's a little jerky on Chrome on OS X, Safari much smoother. Though, Chrome is generally faster with JS -- what gives? Both seem to use the same CPU, so it doesn't seem like Chrome is brute forcing some rendering that Safari is off-loading.
Then again, Chrome is 32 bit and Safari is 64 ... guess that's the diff?
1
Dec 21 '10
It appears identical to me on OS X in Chrome and Safari, so I don't know. What's your CPU speed? It takes a core to 100% in either browser, so perhaps that 100% is not enough for Chrome on your system but it is for Safari. Hmm. I have a 2.53 ghz i5.
1
u/beej71 Dec 21 '10
What, no environment mapping on the ornaments? For shame!
;-)
Very clear writeup on the workings, too! Well done!
1
1
u/chewbacca81 Dec 21 '10
used to do the xmas tree in QuickBasic, back in the day. you kids and your 24-bit color!
1
1
u/kidnees Dec 20 '10
Your code is the most awesome real accomplishment I have ever seen in all of reddit. Congratulations. I hope you are amply rewarded in life for your skill.
For non-programmers: there is no image of a tree. It is all purely created real time by code when yr page loads.
-2
0
-3
-2
-4
u/rjcarr Dec 20 '10
A small niggle:
"and works slow in any current browser except in Chrome"
Why does everyone forget about safari? It worked great for me in safari on a laptop. Do people forget that Apple created Canvas and WebKit, both of which Chrome uses?
5
1
u/jchrome Dec 20 '10
Safari and Chrome do share the (fantastic) Webkit rendering engine (so they both can enjoy the latest HTML 5 and CSS3 stuff), but they do not share the same JavaScript engines. Chrome's JS engine was developed by Google and is far superior to what anyone else has come up with yet. This particular demo is all about JS performance, which FYI is probably why several people down-voted you for your comment.
2
u/rjcarr Dec 20 '10
But it's JS mostly dealing with canvas. If google writes their own implementation of canvas then you'd have a point, but I'm pretty sure they don't.
2
u/ImTheGuyWithTheGun Dec 20 '10
While I don't think you should be downvoted, I also think Safari's success might be limited to Macs. On my fairly-capable Win7 system, Safari5 performs terribly on this demo. Then again, I find Safari on PCs to be sluggish in general. I'm sure it is a different story on a Mac...
1
-4
u/thetanlevel10 Dec 21 '10
Hmm, that's strange. I tried out a ton of browsers to see which loaded the fastest, and ole IE6 won more than 50% of the time. try it for yourself, maybe you'll see why over 40% of the internet is still on that browser.
2
91
Dec 21 '10
[removed] — view removed comment
11
u/ryanjkirk Dec 21 '10
I did the thing directly with "obfuscated" code
That shows (aside from your demo, of course) true command of a language. Even traditionally, when one is able to write a speech in a foreign language, you know you've mastered it.
0
u/TheRedTeam Dec 21 '10
You do know pastebin exists right? ;)
Anyway, this was pretty damn awesome so kudos!
74
u/skelooth Dec 20 '10
Hats off, that is brilliant. Not only is the programming impressive, but the tree looks pretty damn good as well!