try and catch performance
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var collisionCount = 0;
var couple = [];
for(var i = 0 ; i < 100; i++){
couple.push({x:Math.random(),y:Math.random()});
}
//Note: try and catch of a() and c() have the exact same code.
a = function(){
try {
for(var i = 0 ; i < couple.length; i++){
for(var j = 0 ; j < couple.length; j++){
var vx = couple[i].x-couple[j].x;
var vy = couple[i].y-couple[j].y;
if(vx*vx + vy*vy < 0.1)
collisionCount++;
}
}
} catch(err){
}
}
b = function(){
try {
c();
} catch(err){
}
}
c = function(){
for(var i = 0 ; i < couple.length; i++){
for(var j = 0 ; j < couple.length; j++){
var vx = couple[i].x-couple[j].x;
var vy = couple[i].y-couple[j].y;
if(vx*vx + vy*vy < 0.1)
collisionCount++;
}
}
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
a
|
|
pending… |
b
|
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
0 Comments