Show HN: Canvas engines performance comparison – PixiJS, Two.js, and Paper.js
benchmarks.slaylines.io
benchmarks.slaylines.io
https://www.goodboydigital.com/pixijs/bunnymark/
I checked the source briefly, and the problem is that the source draws every single rectangle manually every single tick. That's very inefficient. Just draw a rectangle to a Texture once, and create a bunch of sprites using that Texture. Pixi should be able to handle at least 10x the amount of rectangles if you do this.
(That’s not a statement on how impressive this is, just a “oh that’s how it’s done.”)
I've been using this exact test for years now to judge a phone/tablet before buying/recommending - devices with the exact same SoC can differ wildly in performance.
Nowadays any phone that can't display 20k bunnies at 20FPS likely has:
1. An underpowered GPU.
2. Badly designed cooling.
3. A screen with a too high resolution.
Of course there are many more thorough and appropriate benchmarks, but this one doesn't require you to install anything and will give you an answer before you're approached by the store staff inquiring what is it that you're trying to do with the merchandise.
Given my aging Samsung Galaxy S6 averages about 30FPS for 20k bunnies in Firefox mobile, your criteria might actually be too lax...
Edit: And in Chrome on the same phone I'm in the mid 30's FPS for 50k bunnies.
To give an example: the Sony Xperia Z2 tablet appeared to be decent, because it had the - back then - state of the art Snapdragon 801 SoC.
I think I got less than 10k bunnies on it - no idea why(no power saving mode or anything), but it was a deal breaker for me.
https://www.shadertoy.com/view/Xds3zN
Something with lots of work per pixel to complement Bunnymark's lots of pixels.
One thing that I didn't realize when I first saw it was that the demo is able to render so many sprites because it's doing the simplest possible thing - rendering just a few textures. If you render a bunch of different sprites with different textures, you'll get a big FPS hit.
But then all of your boxes are the same. Which is not what this benchmark is.
We can conclude that Pixi.js is probably faster for drawing sprites, but is definitely slower than Two.js at drawing randomly sized rectangles.
I pushed a fix, the way it was drawing randomly sized rectangles was an unfair comparison. It is much faster than Two.js now (tried it at 10000 rectangles too) and initialises much faster also.
I made a PR to fix it and its much better: https://github.com/slaylines/canvas-engines-comparison/pull/...
It is still not the optimum way to do it in pixi.js but it at least matches the other examples now.
The golden rule is the less you have to clear() and redraw the insides of the graphics the faster it's going to be as pixi.js has to convert your draw commands to triangles to pass to WebGL, if you're just moving the position of something it can use the already calculated triangles and just add an offset before drawing whereas previously you were generating new triangles and hardbaking the position into the triangles each frame.
Because you move each rectangle at a different rate I used a separate graphics instance so that we could move them without having to redraw them.
There would probably be less GC pauses if the benchmark code wasn't doing things like
[...Array(this.count.value).keys()].forEach(...)
instead of a for loop.Functional code might look nice but often creates excess work for the GC and kills performance. We had a situation within the last week where a piece of code was blowing through 350MB of memory unnecessarily, and massively slowing down a heavy set of calculations, because of exactly this kind of issue.
On the contrary, please do this. "productivity" and "readability" are important aspects to consider when writing code, especially if someone else is going to be reading it.
When you've identified a bottleneck, feel free to write the code in the bottleneck more performantly, if necessary. But please do not sacrifice readability across the entire codebase for a couple of hot loops.
It creates an array of length N, but for obscure-to-most-people reasons, Array(N).forEach() doesn't work, so they Rube Goldberged their way to an array that they could call forEach() on. Their solution was to use Array#keys to get an iterable from the array. But an iterable doesn't have a .forEach() method, so they iterate the iterable into another array just to iterate it again with Array#forEach. Frankly the only thing this seems optimized for is to solve the problem without the for-loop for some reason.
The for-loop, on the other hand, is an instantly obvious solution. It's how programmers have been expressing "do this N times" for decades across languages.
Array.from({ length: N }).forEach() for(var i = 0; i < N; i++) {}Unless there's a reason to use Array.forEach such as automatic paralellization or some other SIMD-like optimization that can be done that cannot be done in a forloop.
A lot of cargo-cult programming seem to take functional programming to mean programming using the map/reduce/foreach functions, and you end up with shitty code like [...Array(N).keys()].foreach(), just so you can somehow claim that you're doing functional programming.
The person above wanted a way to actually create an array of length N that could be used with .forEach().
Measuring before optimizing is of course a good idea, but it's also time consuming. There's a lot of latitude to make different reasonable choices about how you write code down in the first place before you get to the measuring staging.
Should we criticize someone who reaches for a for loop because they know it doesn't allocate without proving that that matters more than someone who reaches for a map because they think it's more readable and productive without any great way of proving that that's true?
I mean, doing
sendUserIds(users.map(u => u.id))
is very obviously more readable than what you're forced to do in languages like Go ids := make([]int, len(users))
for i, user := range users {
ids[i] = user.Id
}
sendUserIds(ids)
The difference only grows once you start needing to do more operations, grouping, putting stuff into map by a certain key, etc. Many languages also have lazy sequences for such operations (e.g. https://kotlinlang.org/docs/reference/sequences.html, though they aren't always faster, it depends)The code in the comment above is however not an example of this, and is actually less readable in my opinion. It seems more like an author wishing to be using a different language instead of accepting what they have in front of them.
This is pretty far from premature optimization even if you never measure the effect, it is entirely predictable that code like this would have unnecessarily bad performance. If you write performance sensitive code you don't allocate stuff inside a tight loop unless you can't avoid it. The consequence of this is typically that the code is more straightforward imperative with fewer abstractions, but that is not inherently less readable.
Unless the compiler optimizes it out...
Don't do stuff like pushing rectangles to an array to have their position wrapped, instead of simply doing it right there, unless you want to test the GC.
Correct me if I am wrong.
Nice optimization.
Generally speaking, avoid GC, and avoid setting state that's already set... if you do these two things and use engines that do these two things, it's usually going to be more than fast enough :)
all of these are quite low level engines, nothing wrong with that but there's wrapper around these, like phaser which uses pixi as backend and give quite some useful abstractions on top of the rendering.
Phew, glad I picked Pixi.js for this then. I’m building the next version of Scale of the Universe
(For windows/mac/linux only btw)
What do you mean? It's a website.
I love Paper.js because it has a TON of super useful vector features and is still is reasonably fast :D
But on my flagship iOS device PixiJS got top spot, two.js close behind.
This kind of test is easy work for a well-batched renderer. You can accumulate everything in to a single big typed array, copy to a single vertex buffer, and do one call to drawElements() in WebGL, and bingo, tens of thousands of sprites drawn in one go.
I've got other similar performance tests that can do hundreds of thousands of sprites @ 30 FPS (on a high-end machine), which I believe are bottlenecked mainly on memory bandwidth, because I only managed to make it go faster by reducing the size of the JS objects involved.
Modern JS is ultra fast - if you have the right performant coding style.
I get 10,000 bunnies at 60fps but only 2,000 rectangles.
Results are ... well, Scrawl-canvas isn't Pixi.js fast!
But the results aren't too bad - I can live with that sort of speed. Especially as the library is entirely 2D with no WebGL magic added to the mix.
https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibi...
You could port FB to Canvas and it'd probably be faster until you add a ton of abstraction to give you what HTML does.
Outside of grid-layout, there is nothing salvageable from the CSS layout engine that cannot be done better inside a programmable view such a canvas.
Give it 5 years or so for WebGL/WebGPU to standardize, and new UI libraries with a better layout engine and UI constructs far better than what can be built on top of HTML/CSS will show up.
Nobody thought we would take HTML/CSS as far as we have, but it has already served its purpose.
Your comment reminds me of this article back then: https://engineering.flipboard.com/2015/02/mobile-web
Font rendering for all of unicode is extremely hard.
https://gankra.github.io/blah/text-hates-you/
Multi-language input is extremely hard
https://lord.io/blog/2019/text-editing-hates-you-too/
Having to download an extra meg or 10 of code does not make your website responsive to start, worse if you're updating it constantly so you're users have to re-download that code every few days, hours.
Support for assistive technologies disappears. A page of HTML is relatively easy to scan for text to read or turn to brail or translate to another language. A screen (not a page) of pixels is not.
Similarly extensions all break. Extensions work because there is a known structure to the page (HTML)
UI consistency disappears. Of course pages already have this issue but it will be much much worse if every site rolls it's own pixel rendering GUI because none of the standard keys will work. Ctrl/Cmd-Z for undo Ctrl/Cmd-A for select all? Similarly maybe the user has changed those keys or is using some other assistive device which all works because things are standardized.
Letting the browser handle what's best for the device probably disappears. Cleartype for fonts? Rendering Text or SVG at the user's resolution (yes that can be handled by the page but will it? up to the site)
Password managers break including the browser's built in one. There's no text field to find so no way to know if this is the place to fill them in.
Spell checking breaks. Same as above.
Basically your site will suck for users if you do this. Some will say some frameworks will come up that try to solve all of these issues but that will just mean every pages is on a different version of the framework with different bugs not yet resolved. Sounds like hell.
But don't you think there's a whole lot of apps that could benefit from being on a canvas rather than being slowed down by browser-stuff? Editors come to mind: CodeSandbox and VScode
Sometimes a good compromise is to use canvas for rendering some things things on a page (the way Google Sheets does), but create HTML elements on top as needed for particular behaviour.
It looks to me like the rectangles done in WebGL are drawn at the scaled resolution (1920 x 1080) vs the unscaled resolution (3840 x 2160), whereas the canvas is DPI aware and drawing at the full 4k resolution.
I would need to dig further but basically in WebGL the rectangles are drawn to a texture at the lower res then upscaled just like a straight image of a rectangle prerendered would be at the lower dpi.
Edit:
Looks like paper.js is specifically HiDpi aware and it can be turned off for better performance which would be more fair when compared to the other implementations:
http://paperjs.org/tutorials/getting-started/working-with-pa...
hidpi="off": By default, Paper.js renders into a hi-res Canvas on Hi-DPI (Retina) screens to match their native resolution, and handles all the additional transformations for you transparently. If this behavior is not desired, e.g. for lower memory footprint, or higher rendering performance, you can turn it off, by setting hidpi="off" in your canvas tag. For proper validation, data-paper-hidpi="off" works just as well.
Also PixiJS seems to support HiDpi if resolution is set properly:
https://pixijs.download/dev/docs/PIXI.settings.html
// Use the native window resolution as the default resolution
// will support high-density displays when rendering
PIXI.settings.RESOLUTION = window.devicePixelRatio;