Many mw.config.get calls

JavaScript performance comparison

Revision 2 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  // Based on https://git.wikimedia.org/raw/mediawiki%2Fcore.git/HEAD/resources%2Fsrc%2Fmediawiki%2Fmediawiki.js
  (function($) {

    'use strict';
    var mw,
      hasOwn = Object.prototype.hasOwnProperty,
      slice = Array.prototype.slice;
  
    function Map(values) {
      this.values = values === true ? window : (values || {});
      return this;
    }
  
    Map.prototype = {
      get: function(selection, fallback) {
        var results, i;
        // If we only do this in the `return` block, it'll fail for the
        // call to get() from the mutli-selection block.
        fallback = arguments.length > 1 ? fallback : null;
  
        if ($.isArray(selection)) {
          selection = slice.call(selection);
          results = {};
          for (i = 0; i < selection.length; i++) {
            results[selection[i]] = this.get(selection[i], fallback);
          }
          return results;
        }
  
        if (typeof selection === 'string') {
          if (!hasOwn.call(this.values, selection)) {
            return fallback;
          }
          return this.values[selection];
        }
  
        if (selection === undefined) {
          return this.values;
        }
  
        // invalid selection key
        return null;
      },
  
      /**
       * Sets one or multiple key/value pairs.
       *
       * @param {string|Object} selection String key to set value for, or object mapping keys to values.
       * @param {Mixed} [value] Value to set (optional, only in use when key is a string)
       * @return {Boolean} This returns true on success, false on failure.
       */
      set: function(selection, value) {
        var s;
  
        if ($.isPlainObject(selection)) {
          for (s in selection) {
            this.values[s] = selection[s];
          }
          return true;
        }
        if (typeof selection === 'string' && arguments.length > 1) {
          this.values[selection] = value;
          return true;
        }
        return false;
      }
    };
    mw = {
      Map: Map
    };
    mw.config = new mw.Map(true);
    mw.config.set({
      "example": true,
      "user": "FooBar",
      "ns": 1234
    });
    window.mw = mw;
  }(jQuery));

};
</script>

Preparation code output

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
Single variable
var conf = mw.config.get(['user', 'example', 'ns', 'missing']);
var result = conf.user !== 'Foo' && conf.user !== 'Bar' && conf.user !== 'Baz' && conf.example && conf.ns !== 123 && conf.ns !== 456 && conf.ns !== 789 && !conf.missing;
pending…
No variable
var result = mw.config.get('user') !== 'Foo' && mw.config.get('user') !== 'Bar' && mw.config.get('user') !== 'Baz' && mw.config.get('example') && mw.config.get('ns') !== 123 && mw.config.get('ns') !== 456 && mw.config.get('ns') !== 789 && !mw.config.get('missing');
pending…
Multiple variables
var user = mw.config.get('user');
var ns = mw.config.get('ns');
var result = user !== 'Foo' && user !== 'Bar' && user !== 'Baz' && mw.config.get('example') && ns !== 123 && ns !== 456 && ns !== 789 && !mw.config.get('missing');
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