Array Properties

Benchmark created on


Description

There is some kind of side-effect that we need to perform once when an Array is being accessed.

The idea is to either: keep track of whether or not the side-effect has been executed with a guard or to rewrite the array accessor.

There are two NOOP examples in here as well. They do not perform any additional work and therefore, are just present as a reference.

Setup

window.state = { prop: 0 };
    if(undefined == Array.prototype.a) {
    var sideEffect = function() {
      ++window.state.prop;
    };
    
    Array.prototype.a = function() { sideEffect(); return this; };
    Array.prototype.b = function() {
      if(this.init == true) {
        return this;
      }
      sideEffect(); 
      this.init = true;
      return this;
    };
    Array.prototype.c = function() {
      var t = this;
      sideEffect(); 
      t.c = function() { return t; }
      return t;
    };
    Object.defineProperty(Array.prototype, 'd', {
      get: function() { sideEffect(); return this; },
      enumerable: false
    });
    
    Object.defineProperty(Array.prototype, '$init$', {
      value: false,
      enumerable: false
    });
    
    Object.defineProperty(Array.prototype, 'e', {
      get: function() { 
        if(this.$init$) { return this; }
        sideEffect();  
        this.$init$ = true;
        return this;
      },
      enumerable: false
    });
    
    Object.defineProperty(Array.prototype, 'f', {
      get: function() { 
        Object.defineProperty(this, 'f', {
          value: this,
          enumerable: false,
          configurable: false
        });
        sideEffect(); 
        return this;
      },
      enumerable: false,
      configurable: true
    });
    }
    
    var N = 1000;
    
    var arr = [];
    var brr = [];
    var i = 0;
    var CONTROL = 0|0;
    for(;i<N;++i) {
      CONTROL = (CONTROL + i | 0) + i | 0;
      arr.push(i);
      brr.push(i);
    }

Teardown


    if(window.state.prop != 2) {
      //console.log("test ko!");
    } else {
      //console.log("test ok!");
    }
    
    delete window.state;
    
  

Test runner

Ready to run.

Testing in
TestOps/sec
No Condition in Function
var n = N;
var s = 0;
while(--n > -1) {
  s = (s + arr.a()[n] | 0) + brr.a()[n] | 0;
}
if(s != CONTROL) { console.log("err in 1"); }
ready
Condition in Function
var n = N;
var s = 0;
while(--n > -1) {
  s = (s + arr.b()[n] | 0) + brr.b()[n] | 0;
}
if(s != CONTROL) { console.log("err in 2"); }
ready
Re-Write Function
var n = N;
var s = 0;
while(--n > -1) {
  s = (s + arr.c()[n] | 0) + brr.c()[n] | 0;
}
if(s != CONTROL) { console.log("err in 3"); }
ready
No Condition in Property
var n = N;
var s = 0;
while(--n > -1) {
  s = (s + arr.d[n] | 0) + brr.d[n] | 0;
}
if(s != CONTROL) { console.log("err in 4"); }
ready
Condition in Property
var n = N;
var s = 0;
while(--n > -1) {
  s = (s + arr.e[n] | 0) + brr.e[n] | 0;
}
if(s != CONTROL) { console.log("err in 5"); }
ready
Re-Write Property
var n = N;
var s = 0;
while(--n > -1) {
  s = (s + arr.f[n] | 0) + brr.f[n] | 0;
}
if(s != CONTROL) { console.log("err in 6"); }
ready

Revisions

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