in operator

JavaScript performance comparison

Test case created by WebReflection

Preparation code

<script>
  // all functions should avoid JIT optimizations
  // (e.g. if nothing happens JIT may nullify function body)
  // hopefully assigning the object to a non GC global variable
  // will do the trick ... let's see ... 
  
  function inOperator(object) {
      "prop" in object || (object.prop = {});
      return object;
  }
  
  function commonPattern(object) {
      object.prop = object.prop || {};
      return object;
  }
  
  function betterPattern(object) {
      object.prop || (object.prop = {});
      return object;
  }
  
  var
      // simple shim that *should* fail in old browsers
      // if getters/setters are not supported
      defineProperty = Object.defineProperty || function (o, k, v) {
          o.__defineGetter__(k, v.get);
          o.__defineSetter__(k, v.set);
          return o;
      },
      globalResult
  ;
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
in operator set
globalResult = inOperator({});
pending…
in operator
globalResult = inOperator({prop:{}});
pending…
in operator + magic set
globalResult = inOperator(defineProperty({}, "prop", {
    get: function () {
        return this._prop;
    },
    set: function (_prop) {
        // here something more meaningful ...
        this._prop = _prop;
    }
}));
pending…
in operator + magic
globalResult = inOperator(defineProperty({}, "prop", {
    get: function () {
        return this._prop || {}; // let's say the default
    },
    set: function (_prop) {
        // here something more meaningful ...
        this._prop = _prop;
    }
}));
pending…
common pattern set
globalResult = commonPattern({});
pending…
common pattern
globalResult = commonPattern({prop:{}});
pending…
common pattern + magic set
globalResult = commonPattern(defineProperty({}, "prop", {
    get: function () {
        return this._prop;
    },
    set: function (_prop) {
        // here something more meaningful ...
        this._prop = _prop;
    }
}));
pending…
common pattern + magic
globalResult = commonPattern(defineProperty({}, "prop", {
    get: function () {
        return this._prop || {}; // let's say the default
    },
    set: function (_prop) {
        // here something more meaningful ...
        this._prop = _prop;
    }
}));
pending…
better pattern set
globalResult = betterPattern({});
pending…
better pattern
globalResult = betterPattern({prop:{}});
pending…
better pattern + magic set
globalResult = betterPattern(defineProperty({}, "prop", {
    get: function () {
        return this._prop;
    },
    set: function (_prop) {
        // here something more meaningful ...
        this._prop = _prop;
    }
}));
pending…
better pattern + magic
globalResult = betterPattern(defineProperty({}, "prop", {
    get: function () {
        return this._prop || {}; // let's say the default
    },
    set: function (_prop) {
        // here something more meaningful ...
        this._prop = _prop;
    }
}));
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.

0 Comments