Doom on the Web

Update: We had a doubt whether this port of the Open Source Doom respected its term of use. We decided to remove it from our Website before taking an informed and definitive decision.

This is a guest post written by Alon Zakai. Alon is one of the Firefox Mobile developers, and in his spare time does experiments with JavaScript and new web technologies. One of those experiments is Emscripten, an LLVM-to-JavaScript compiler, and below Alon explains how it uses typed arrays to run the classic first-person shooter Doom on the web.

As a longtime fan of first-person shooters, I’ve wanted to bring them to the web. Writing one from scratch is very hard, though, so instead I took the original Doom, which is open source, and compiled it from C to JavaScript using Emscripten. The result is a version of Doom that can be played on the web, using standard web technologies.

Doom renders by writing out pixel data to memory, then copying that pixel data to the screen, after converting colors and so forth. For this demo, the compiled code has memory that is simulated using a large JavaScript array (so element N in that array represents the contents of memory address N in normal native code). That means that rendering, color conversion, and copying to the screen are all operations done on that large JavaScript array. Basically the code has large loops that copy or modify elements of that array. For that to be as fast as possible, the demo optionally uses JavaScript typed arrays, which look like normal JavaScript arrays but are guaranteed to be flat arrays of a particular data type.

// Create an array which contains only 32-bit Integers
var buffer = new Int32Array(1000);
for ( var i = 0 ; i < 1000 ; i++ ) {
    buffer[i] = i;
}

When using a typed array, the main difference from a normal JavaScript array is that the elements of the array all have the type that you set. That means that working on that array can be much faster than a normal array, because it corresponds very closely to a normal low-level C or C++ array. In comparison, a normal JavaScript array can also be sparse, which means that it isn't a single contiguous section of memory. In that case, each access of the array has a cost, that of calculating the proper memory address. Finding the memory address is much faster with a typed array because it is simple and direct. As a consequence, in the Doom demo the frame rate is almost twice as fast with typed arrays than without them.

Typed arrays are very important in WebGL and in the Audio Data API, as well as in Canvas elements (the pixel data received from getImageData() is, in fact, a typed array). However, typed arrays can also be used independently if you are working on large amounts of array-like data, which is exactly the case with the Doom demo. Just be careful that your code also works if the user's browser does not support typed arrays. This is fairly easy to do because typed arrays look and behave, for the most part, like normal ones — you access their elements using square brackets, and so forth. The main potential pitfalls are:

  • Typed arrays do not have the slice(). Instead they have the subarray(), which does not create a copy of the array — instead it's a view into the same data.
  • Don't forget that the type of the typed array is silently enforced. If you write 5.25 to an element of an integer-typed array and then read back that exact same element, you get 5 and not 5.25.

About louisremi

Developer Relations Team, long time jQuery contributor and Open Web enthusiast. @louis_remi

More articles by louisremi…


14 comments

  1. Shmerl

    Impressive!

    There is another very interesting project worth noting, which also heavily uses typed arrays – Javascript PC Emulator:

    http://bellard.org/jslinux/
    http://bellard.org/jslinux/tech.html

    June 3rd, 2011 at 09:13

  2. Not found

    The link gives me a not found error.

    June 4th, 2011 at 02:09

  3. nemo

    Doom on the web seems to 404 now?

    June 4th, 2011 at 12:31

    1. skierpage

      Sadly it’s all 404 Google cache still has the intro page but not the launch page or download, and the awful MDN search has a link to the upload (
      developer.mozilla.org/media/uploads/demos/a/z/azakai/487d42c2ecc1627745a469861bd377e2/doom-on-the-web_1306727266_demo_package/details.html ), which is also a 404.

      June 4th, 2011 at 13:41

  4. Mike

    Bah, what a tease. Really wanted to see the Doom demo. ARGH! I won’t believe any of this until I see the demo running on my browser.

    June 4th, 2011 at 18:39

  5. nemo

    Supposedly DMCA’d :(

    June 5th, 2011 at 12:23

  6. farin

    Brand new technology typed arrays – so javascript can be used as in C progs, with untyped arrays it is unusable. Reinventing the wheel.

    June 9th, 2011 at 03:26

    1. louisremi

      It so happens that you are browsing a usable Web made of pages embedding Javascript and not using typed arrays.

      June 9th, 2011 at 03:32

  7. ZippoLag

    Come on! there can’t be a copyright issue with DOOM!! Let us play it!

    June 14th, 2011 at 08:17

  8. Janet Swisher

    The DCMA takedown request is available here:
    https://wiki.mozilla.org/Legal/Infringement_Notices/3_June_2011

    June 17th, 2011 at 13:00

  9. jmdesp

    I think this can be reinstated, by describing it as playing the Doom Engine on-line, and using a non-commercial wad, I don’t know which one is the best to use, but there’s a good number of them :
    http://en.wikipedia.org/wiki/Doom_WAD#Notable_WADs
    http://www.doomworld.com/idgames/index.php?top

    Freedoom would be nice but it require the Boom version of the source :
    http://en.wikipedia.org/wiki/Doom_source_port#Boom_and_derivations

    June 20th, 2011 at 08:01

  10. James Haley

    At least keep your source code up somewhere; Zenimax has absolutely no right to tell you that you cannot distribute it as it is protected by the GPL, which they absolutely cannot revoke.

    Personally I think the shareware distribution license also gives ground to issue a DMCA counter-notice, but it might be a gray enough area to result in a court case. One worth fighting maybe, if you had the dough.

    As the author of a DOOM source port (the Eternity Engine), I must now fear that the Zenimax money mongers will be coming after me and the rest of the DOOM community soon.

    July 20th, 2011 at 20:40

    1. louisremi

      The game engine has been open-sourced, not the game assets AFAIK.

      July 21st, 2011 at 07:46

      1. James Haley

        The program was using the shareware IWAD, which although it is not under a libre license, has always stated it could be redistributed.

        Zenimax doesn’t know the meaning of the word “share” in shareware, apparently.

        August 5th, 2011 at 16:03

Comments are closed for this article.