deProtected vs __proto__

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var def     = Object.defineProperty,
        slice   = Array.prototype.slice,
        methods = ['push','pop','shift','unshift','splice','sort','reverse']
   
    // The proxy prototype to replace the __proto__ of
    // an observed array
    var ArrayProxy = Object.create(Array.prototype)
   
    // Define mutation interceptors so we can emit the mutation info
    methods.forEach(function (method) {
        ArrayProxy[method] = function () {
            var result = Array.prototype[method].apply(this, arguments)
            this.__observer__.emit('mutate', this.__path__, this, {
                method: method,
                args: slice.call(arguments),
                result: result
            })
            return result
        }
    })
   
    ArrayProxy.remove = function (index) {
        if (typeof index !== 'number') index = this.indexOf(index)
        return this.splice(index, 1)[0]
    }
       
    ArrayProxy.replace = function (index, data) {
        if (typeof index !== 'number') index = this.indexOf(index)
        return this.splice(index, 1, data)[0]
    }
       
    ArrayProxy.mutateFilter = function (fn) {
        var i = this.length
        while (i--) {
            if (!fn(this[i])) this.splice(i, 1)
        }
        return this
    }
   
    function defProtected (obj, key, val) {
        if (obj.hasOwnProperty(key)) return
        def(obj, key, {
            enumerable: false,
            configurable: false,
            value: val
        })
    }
};
</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
defProctected()
var a = []
for (var key in ArrayProxy) {
    defProtected(a, key, ArrayProxy[key])
}
pending…
__proto__
var a = []
a.__proto__ = ArrayProxy
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