r/programming • • Dec 20 '10

A Christmas Tree in 1k of Javascript

http://www.romancortes.com/blog/how-i-did-the-1kb-christmas-tree/
1.1k Upvotes

221 comments sorted by

View all comments

Show parent comments

8

u/[deleted] 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.

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.

5

u/[deleted] Dec 21 '10

hint: a sprite is a textured polygon. you can very much take advantage of hardware acceleration using just sprites.

Although i doubt this tree example uses any hardware acceleration.

-2

u/ascendant512 Dec 21 '10

Yeah, thanks for the comment. I know that you can hardware-accelerate sprites by turning them into a textured polygon. Hardware accelerated sprites seem to be both common and rare. The only example I can think of is the mouse pointer.

It's just that usually when you see a game or something that uses sprites, it's not 3D accelerated. Even more so the older it is.

Perhaps someone can write a WebGL canvas version of this toy. It'll probably be faster >_>

2

u/ZorbaTHut Dec 21 '10

Actually, the vast majority of modern 2d games are hardware-accelerated. Software rendering is long since dead. I'd say you've got a 95%+ chance of any 2d game in the last five years being hardware-accelerated.

That's probably an underestimation.

0

u/ascendant512 Dec 21 '10

The vast majority of what you think are modern 2D games are not 2D. They are rendered in a 3D environment with a fixed camera perspective using 3D textured polygons (again, not sprites); not because it's easier to do it that way but because it's much, much faster. The rest of them use non-hardware accelerated java or non-hardware accelerated flash. Which use sprites. Which use the CPU.

You are wrong, go away.

5

u/phort99 Dec 21 '10

So, because hardware-accelerated 2D games use some GPU tricks that involve them being rendered in the same manner as 3D graphics, that makes them suddenly 3D? All it is is a different architecture for doing exactly the same thing. Because a shader and a textured quad get involved, suddenly it's 3D?

WebGL is not the only hardware acceleration tech making its way into browsers, canvas is also getting hardware acceleration. IE9 and chrome both use hardware accelerated canvas, from what I understand.

What exactly is the point you're trying to make? You started out arguing that hardware accelerated javascript does not exist. I don't see anybody trying to make the argument that it does.

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.

This comment was probably not talking about hardware-accelerating javascript, it was talking about the output that javascript controls: when the script says "draw this image here," does the CPU set each pixel or does the GPU?

→ More replies (2)

5

u/ZorbaTHut Dec 21 '10

So what exactly is the difference between a 2D game and a game "rendered in a 3D environment with a fixed camera perspective using 3D textured polygons"?

If it looks like a duck and quacks like a duck, it's a duck. I'd call that a 2D game.

→ More replies (6)
→ More replies (1)

3

u/ZorbaTHut Dec 21 '10

Hardware Acceleration is Coming to Chrome

You're somewhat out of date. They're trying to get hardware acceleration working for things as simple as webpage layouts. No, the javascript isn't accelerated, but the actual compositing of the sprites certainly would be. And that tree uses a shitload of sprites with alpha channels.

0

u/ascendant512 Dec 21 '10

Take a closer look at the link you posted. It still has nothing to do with javascript, and everything to do with what I said it did. The examples at the bottom of this description and the description itself say that the acceleration's focus is 1. HTML5 and 2. CSS3.

This is in addition to the original hardware acceleration which copies parts of a page already rendered by the CPU directly into the GPU framebuffer. The CPU still does the javascript, and the javascript still does the semitransparent sprites.

4

u/ZorbaTHut Dec 21 '10

Read your own comment again.

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.

You're right that there's no hardware-accelerated javascript. Everything else in that paragraph is wrong. Chrome's hardware acceleration isn't WebGL, this acceleration isn't based on <canvas>, and Chrome is working on having significantly more hardware acceleration than Microsoft Powerpoint.

Hardware acceleration doesn't imply polygons. You can do it with sprites also. That's what the Chrome team is working on.

And just to stave off the inevitable, while it might not be accelerating Javascript itself, it certainly can accelerate the sprites that are placed by Javascript.

→ More replies (4)

1

u/[deleted] Dec 21 '10

Just to clear up, I didn't mean that the browser renders polys, just an example of cpu vs. gpu usage. And I was only wondering if it were the case. I'm not even using chrome, fyi so i'm by no means a fan boy. Matter of fact I have yet to install and try it.

But anyway, thank you for the further info. I know about open GL but not WebGL. I'm going to check that out.

3

u/ascendant512 Dec 21 '10

I'm sorry for calling you a fanboy.

It is probably impossible to hardware accelerate JS, at least not without severely cutting its usefulness. However, it's not really that bad. All the fast javascript engines use JIT (which attempts to make it run as fast as natively compiled code if you don't know), and JS by its nature as a general purpose scripting language is more suited to a general purpose processor anyway.

1

u/techdawg667 Dec 21 '10

then the speed advantage of chrome over firefox is ever more awesome now!

→ More replies (2)

1

u/funnynickname Dec 21 '10

I've got a single core 2.8ghz with 3 gigs and firefox just pinned at 100% then crashed. Chrome renders it flawlessly and fast. I don't believe it is hardware acceleration. Chrome is just seriously optimized for this stuff, etc. Translates the code in to machine code very efficiently.

2

u/mdot Dec 21 '10

So there is some way that Chrome can invoke hardware acceleration that the others cannot?

Not trying to smart ass, because I honestly want to know the answer. But hardware acceleration doesn't explain different CPU usage, using different browsers, on the same machine.

3

u/[deleted] Dec 21 '10

Yes, they each need to implement hardware acceleration, its not just something that comes for free.

1

u/mdot Dec 21 '10

Ok, I understand that you can't get something for nothing.

However, is it then, your contention, that Chrome is the only browser that explicitly makes use of hardware acceleration? Seems pretty far-fetched to me.

1

u/[deleted] Dec 21 '10

I didn't say chrome is the only one doing it. They're all doing it, but it's a recent development so everyone has different bits done, and they each have a ways to go.

Also, "hardware acceleration" is a pretty blanket term for a lot of things. It could mean webGL, it could mean compositing using GL, it could mean rendering the page using tiles, etc.

1

u/mdot Dec 21 '10

Thanks for the info...TIL.

1

u/[deleted] Dec 21 '10

Well I can't confirm, it's just a hunch of mine. Not much else can explain the difference in performance, other than maybe a bigger memory, buffer or cache allocation for javascript. Not really sure what else could account for the performance difference.

I'd like to know too btw if anyone else can chime in.

1

u/ImTheGuyWithTheGun Dec 21 '10

I might just be ignorant, but I attribute it simply to Chrome's V8 javascript engine, which tends to be faster than other browser's engines. From the wiki:

V8 increases performance by compiling JavaScript to native machine code before executing it, rather than to a bytecode or interpreting it. Further performance increases were achieved by employing optimization techniques such as inline caching. With these features, JavaScript applications running within V8 have an effective speed comparable to a compiled binary

1

u/[deleted] Dec 21 '10

Ah! there it is. The proper info. Thank you for posting! That makes a hell of a lot more sense.

1

u/TouchedByAnAnvil Dec 21 '10

i'm suspecting that hardware acceleration has a lot to do with it.

I'm suspecting you don't know what you're talking about.

Hardware accelaration only works for large amounts of very similar calculations (eg rendering triangles)

Javascript is interpreted code, it needs to run on a general purpose CPU. There is no hardware accelarated javascript. There are only faster software implementations.