typeofnum

Benchmark created by mattdesl on


Setup

function Vec2(x, y) {
        this.x = x||0;
        this.y = y||0;
    }
    
    Vec2.prototype.set1 = function(x, y) {
        this.x = x;
        this.y = y;
        return this;
    };
    
    Vec2.prototype.set2 = function(x, y) {
        if (x && (x.x === 0 || x.x)) {
            this.x = x.x;
            this.y = x.y;
        } else {
            this.x = x;
            this.y = y;
        }
        return this;
    };
    
    Vec2.prototype.set3 = function(x, y) {
        if (typeof x === "object") {
            this.x = x.x;
            this.y = x.y;
        } else {
            this.x = x;
            this.y = y;
        }
        return this;
    };
    
    var v = new Vec2(Math.random(), Math.random());
    var v2 = new Vec2(Math.random(), Math.random());

Test runner

Ready to run.

Testing in
TestOps/sec
nocheck
v.set1(20, 50);
ready
check1-obj
v.set2(v2);
ready
check1-xy
v.set2(20, 50);
ready
check2-obj
v.set3(v2);
ready
check2-xy
v.set3(20, 50);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by mattdesl on