object properties

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var idmap = {};
   
    var propsByPropObjId = {};
   
    function FnPropObj(id) {
        this.id = id;
    }
    FnPropObj.prototype.id = 0;
    Object.defineProperty(FnPropObj.prototype, 'prop', {
        get: function getProp() {
                return propsByPropObjId[this.id];
        },
        set: function setProp(prop) {
                propsByPropObjId[this.id] = prop;
        },
        configurable: false,
        enumerable: true
    });
   
   
    function FnObj(id) {
        this.id = id;
    }
    FnObj.prototype.id = 0;
    FnObj.prototype.getProp = function getProp() {
        return propsByPropObjId[this.id];
    };
    FnObj.prototype.setProp = function setProp(prop) {
        propsByPropObjId[this.id] = prop;
    };
   
   
    function PropObj(id) {
        this.id = id;
        this.prop = Math.random();
    }
    PropObj.prototype.id = 0;
    PropObj.prototype.prop = 0;
   
   
    for (var i = 0; i < 1000; ++i) {
        propsByPropObjId[i] = Math.random();
    }
   
    function timeit(Class, get) {
        var arr = [];
        for (var i = 0; i < 1000; ++i) {
                arr[i] = new Class(i);
        }
        if (get) {
                for (var i = 0; i < 1000; ++i) {
                        arr[i].setProp(1 - arr[i].getProp());
                }
        } else {
                for (var i = 0; i < 1000; ++i) {
                        arr[i].prop = 1 - arr[i].prop;
                }
        }
    }
};
</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
fnprop
timeit(FnPropObj, false);
pending…
fnobj
timeit(FnObj, true);
pending…
propobj
timeit(PropObj, false);
pending…

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

Compare results of other browsers

0 comments

Add a comment