Hi guys,
as I'm planning to create an RTS game, I was searching for a modern benchmark to compare on how other technologies stack up with AS3 and if JS was still slower in a simple real-life code, as some benchmarks pointed that it was twice as slow and some, more recent, that up to 5 times faster than AS3.
So I've created my simple, not deeply optimized code of units collisions and ported it to Flash/AS3, Typescript (NodeJs + Vite) and most recent .NET.
Yes, there's still a place to optimize, but it's not a real game code yet, just to compare relative performance.
I've used Vector in flash as it's the fastest in Flash.
unitCount = 2000;
var i:int = this.unitCount;
while (i > 0) {
--i;
var unit1:Unit = this.units[i];
var j:int = this.unitCount;
while (j > 0) {
--j;
var unit2:Unit = this.units[j];
if (unit1 === unit2) continue;
var distance:Number = Math.abs(this.distance(unit1.x, unit1.y, unit2.x, unit2.y));
var maxRadius:Number = Math.max(unit1.radius, unit2.radius);
if (distance < maxRadius)
unit1.moveAwayFrom(unit2);
}
}
private function distance(x1:Number, y1:Number, x2:Number, y2:Number):Number {
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
class Unit {
internal var x:Number = Math.random() * 800;
internal var y:Number = Math.random() * 600;
internal var radius:Number = 2 + Math.random() * 3;
public function moveAwayFrom(unit:Unit):void {
var speedX:Number = (unit.x - this.x) * .1;
var speedY:Number = (unit.x - this.x) * .1;
this.x -= speedX;
this.y -= speedY;
unit.x += speedX;
unit.y += speedY;
}
}
And I was a bit shocked about the results:
So we have 2000 * 2000 = 4 000 000 of collisions (with square root) and some other calculations.
For smooth 60 FPS we need no more than 0.01667 sec. per frame
Flash with Vectors: from 0.381sec to 0.649sec. (launched in Animate and in IDEA)
Typescript with Arrays: 0.03sec. with Arrays (launched in Chrome via NodeJs and Vite)
Typescript with hash Set: approx. 0.01 sec.
.NET with Arrays: 0.022sec - 0.038sec depending on x32 (faster) x64 (slower) and standalone app build
.NET with hash set was up to 50% faster than TS with hash set, I did not save the numbers, though
Unity still not tested, I'm pretty sure with Burst Compiler it will win.
Tested on Ryzen 5 3600 CPU and Windows 11
So JS looks at least 10 times faster than AS3, and in a worst scenario up to 65 times?
What are your conclusions? Perhaps, my tests have a huge flaw I do not see, or flash is indeed not a performant option at all comparing to other technologies?