At a talk I gave at the recent HTML5 Developers Conference in San Francisco, I covered some of the performance issues we had whilst building the Playcraft Engine. Notably, trying to get a working class framework with static inheritance, as well as memory issues associated with excessive object garbage collection in Javascript virtual machines (JVM’s).
I think we solved those problems in a pretty cool way, and decided to open-source the code (check it out on https://github.com/playcraft/gamecore.js) as gamecore.js.
Using gamecore.js for object pooling, we’ve seen massive reductions in garbage collection, and a reduction in memory footprint. In one of our demo games, a high-speed space shooter, garbage collection was reduced 95% — resulting in around a 1200% jump in performance — simply by subclassing from gamecore.Pooled.

Here’s a quick overview.
gamecore.js is made up of 5 components:
- gamecore.Base – an implementation of class.js (static inheritance, super methods and introspection) with some added bug fixes as well as per class (simple) logging, and unique object ids.
- gamecore.Pooled – garbage collection bad, automatic super-fast object pooling good!
- gamecore.LinkedList – a high-performance double linked list
- gamecore.Device – a device independent map (well, a start at one anyway)
- gamecore.PerformanceMeasure – a first stab at a simple performance collector with shimming.
- gamecore.Hashtable – Tim Down’s awesome hashtable.
gamecore.js isn’t a game engine; it’s just a set of foundation classes you can use to build cool game code, and not have to worry about things like the garbage collector. We’re also hoping that others building libraries intended for gaming take advantage of the object pooling to save us all time in the future.
Some quick examples:
Create a class
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
var Fighter = gamecore.Base.extend('Fighter',
{
// static (this is inherited as well)
firingSpeed: 1000
},
{
// instance
hp: 0,
init: function(hp) // instance constructor
{
this.hp = hp;
},
fire: function()
{
this._super(); // super methods!
// do firing!
}
});
var gunship = new Fighter(100); |
Inherit through subclassing
|
1 2 3 4 5 6 7 8 9 |
// inherit from the fighter class
var HeavyFighter = Fighter.extend('HeavyFighter',
{
// override the (static) firing delay (rapid fire)
firingSpeed: 300
},
{ });
var heavyGunship = new HeavyFighter(500); |
Super easy object pooling
gamecore.Pooled will handle the construction and assignment
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// derive from gamecore.Pooled, instead of gamecore.Base
var Fighter = gamecore.Pooled.extend('Fighter',
{
create: function(hp)
{
var n = this._super(); // acquire from the pool
n.hp = hp; // (re)setup the object instance
return n;
}
},
{
...
});
var gunship = Fighter.create(100); // use .create not new
gunship.release(); // hand it back to the pool
// get a list of free or used objects (as gamecore.LinkedList's)
var inUse = Fighter.getPool().usedList;
var free = Fighter.getPool().freeList; |
Doubly Linked Lists
Super fast for adding, removing, looping with low memory overhead
|
1 2 3 4 5 6 7 8 9 10 11 |
var list = new gamecore.LinkedList();
list.add(gunship);
var next = list.first();
while( next )
{
next.obj.draw( ... );
next = next.nextLinked;
}
list.remove(gunship); |
There’s more to explore in gamecore.js, including a device map (for quick shimming of device details) and a cool hashmap. Feel free to play around and give us feedback.
You can access gamecore.js on github.
If you’re looking for a complete HTML5/Javascript game engine then join the Playcraft Engine beta.
James June 25, 2012 - 4:49 am
This is awesome. I’ve been using the pooling for a week or so now and I have to say it’s one of the best solutions I’ve seen. Super easy.
Eliot Graham June 25, 2012 - 9:08 pm
Noticed you guys are using a node extension object for linkedlist items. This seems to create overhead; why not add properties to the contained object?
marty June 25, 2012 - 9:17 pm
That’s a great observation. We debated that quite a bit internally; basically not wanting to play with objects; i.e. storing an object in the linkedlist doesn’t modify it.
You’re right though, that this would save on memory (no need for node objects), but also increase performance, since the nodes would not need to be looked up on access.
We’re thinking about doing it as an option, say appending something like __gc__[LinkedList.uniqueID]_next / prev to the contained object.
Eliot Graham June 26, 2012 - 3:19 pm
Yes, that would work well.
K. J. June 26, 2012 - 3:19 pm
Nice idea. Any way to integrate without having to use the class structure?
marty June 26, 2012 - 8:17 pm
I think so, however it was structurally much easier given gamecore.Class’s static inheritance (the factory constructors).
לינקים שמעניינים אנשים שמפתחים | קודי July 7, 2012 - 3:59 pm
[...] Gamecore.js - פריימוורק למשחקי Javascript שטוענת שהצליחה לשפר ב-1200% את הביצועים של משחק מסויים שהם פיתחו. נראה מבטיח. [...]
Meine Lesezeichen vom 29 Juni bis 13 Juli : Casa Rock! July 13, 2012 - 2:25 pm
[...] Gamecore.js: increase javascript game performance by 1200% | Playcraft [Tags: gamedev class framework javascript performance ] Veröffentlicht: 13. Juli, 2012 Kategorie(n): casa.licous Tags: casa.licous « Previous Post [...]