Many mw.config.get calls (v3)

Revision 3 of this benchmark created by Helder on


Preparation HTML

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

Setup

// 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({
        "A": "a",
        "B": "b",
        "C": "c"
      });
      window.mw = mw;
    }(jQuery));

Test runner

Ready to run.

Testing in
TestOps/sec
Single variable
var conf = mw.config.get(['A', 'B', 'C']);
var result = conf.A !== 1 && conf.A !== 2 && conf.A !== 3 && conf.A !== 4 && conf.A !== 5 && conf.A !== 6 && conf.B !== 1 && conf.B !== 2 && conf.B !== 3 && conf.B !== 4 && conf.B !== 5 && conf.B !== 6 && conf.C !== 1 && conf.C !== 2 && conf.C !== 3 && conf.C !== 4 && conf.C !== 5 && conf.C !== 6
ready
No variable
var result =
  mw.config.get('A') !== 1 && mw.config.get('A') !== 2 && mw.config.get('A') !== 3 && mw.config.get('A') !== 4 && mw.config.get('A') !== 5 && mw.config.get('A') !== 6 && mw.config.get('B') !== 1 && mw.config.get('B') !== 2 && mw.config.get('B') !== 3 && mw.config.get('B') !== 4 && mw.config.get('B') !== 5 && mw.config.get('B') !== 6 && mw.config.get('C') !== 1 && mw.config.get('C') !== 2 && mw.config.get('C') !== 3 && mw.config.get('C') !== 4 && mw.config.get('C') !== 5 && mw.config.get('C') !== 6
ready
Multiple variables
var a = mw.config.get('A'),
  b = mw.config.get('B'),
  c = mw.config.get('C');
var result =
  a !== 1 && a !== 2 && a !== 3 && a !== 4 && a !== 5 && a !== 6 && b !== 1 && b !== 2 && b !== 3 && b !== 4 && b !== 5 && b !== 6 && c !== 1 && c !== 2 && c !== 3 && c !== 4 && c !== 5 && c !== 6
ready

Revisions

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