Recently, I was tasked with learning ReactJS (or just "React"). So, I made the most obvious choice possible: I decided to write an ASCII roguelike, which uses React as the front-end.
Why? For several, obvious reasons:
I set out to create, in my mind, the first step of any text roguelike: draw text on the screen really fast and see how it performs. HTML should render at lightning-speed in my modern browser, right?
Wrong.
React is just a front-end technology; it doesn't dictate how I should structure my HTML. With that in mind, I set out to answer two questions:
div
tags vs. span
tags, etc. make any difference?In brief, the answer to question #2 ended up being "no." Performance bottlenecks on something else entirely; none of those changes make any significant difference.
I wrote a little Javascript that tests exactly what I want:
div
tag for each tile. Style it appropriately (eg. width/height big enough to fit any character)You can see the code, more or less (with FPS counting), here. It's simple, and to the point.
In a word: abysmal! On my fairly beefy dev machine (lots of RAM, good CPU, mediocre GPU), it renders at a measly 6-7FPS. You can see the results in this tweet.
As I mentioned earlier, I tried several variations; none of them improve performance, at all. The core of it comes down to a call to set the character itself. Pseudocode:
var character = ...
var colour = ...
var tile = window.tiles[i];
tile.innerText = character
It turns out that browsers, even modern ones, even on beefy hardware, are really, really slow when you set innerText
. The only alternative - using innerHTML
-- is slower, and probably broken on Internet Explorer.
For an ASCII roguelike without too much going on, 6FPS is probably enough. If I really cared about performance, I could switch to canvas-rendering and a bitmap font (lots of work and not sure how it works with React), or using images - either images of text, or real images.
For me, the goal is to learn ReactJS, so I plan to continue forward with this as-is, without major surgery.
Liked our updates? Subscribe to our newsletter to get access to our game demos and exclusive insider updates!