box intersect

JavaScript performance comparison

Test case created by ly tran

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var BB = function (width, height) {
        var hw = width / 2.0;
        var hh = height / 2.0;
   
        this.l = -hw;
        this.b = -hh;
        this.r = hw;
        this.t = hh;
    };
   
    BB.prototype.intersects1 = function (b) {
        var a = this;
        return (a.l <= b.r && b.l <= a.r && a.b <= b.t && b.b <= a.t);
    };
   
    BB.prototype.intersects2 = function (b) {
        var a = this;
        return !(a.l > b.r && b.l > a.r && a.b > b.t && b.b > a.t);
    };
   
    BB.prototype.not_intersects = function (b) {
        var a = this;
        return (a.l > b.r && b.l > a.r && a.b > b.t && b.b > a.t);
    };
   
    var boxes = []
    for (var i = 0; i < 100; i++) {
      boxes.push(new BB(Math.random() * 1000, Math.random() * 1000))
    }
    for (var i = 0; i < 100; i++) {
      boxes.push(new BB(-Math.random() * 1000, -Math.random() * 1000))
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
1
for (var i = 0; i < boxes.length; i++) {
  for (var j = 0; j < boxes.length; j++) {
    boxes[i].intersects1(boxes[j])
  }
}
pending…
2
for (var i = 0; i < boxes.length; i++) {
  for (var j = 0; j < boxes.length; j++) {
    boxes[i].intersects2(boxes[j])
  }
}
pending…
3
for (var i = 0; i < boxes.length; i++) {
  for (var j = 0; j < boxes.length; j++) {
    boxes[i].not_intersects(boxes[j])
  }
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment