teststoring

JavaScript performance comparison

Test case created

Preparation code

<script>
var Store = function Store() {
  var values = [];
  var keys = [];

  var remove = function remove(array, from, to) {
    var rest = array.slice((to || from) + 1 || array.length);
    array.length = from < 0 ? array.length + from : from;
    return array.push.apply(array, rest);
  };

  var removeItem = function removeItem(index) {
    remove(values, index);
    remove(keys, index);
  };

  var addItem = function addItem(key, val) {
    values.push(val);
    keys.push(key);
  };

  var keyIndex = function keyIndex(key) {
    var val;
    for (var i = 0, len = keys.length; i < len; i++) {
      if (keys[i] === key) { val = i; }
    }
    return val;
  };

  this.get = function(key) {
    var val;
    for (var i = 0, len = keys.length; i < len; i++) {
      if (keys[i] === key) { val = values[i]; }
    }
    return val;
  };

  this.set = function(key, val) {
    var index = keyIndex(key);
    if (index > -1) {
      removeItem(index);
    }
    addItem(key, val);
  };

};


var SimpleStore = function SimpleStore(){
  var store = {};

  this.get = function get(key){
    return store[key];
  };
  this.set = function set(key, val){
    store[key] = val;
  };
};

var a = new Store();
var b = new SimpleStore();
a.set('apple1', {});
a.set('apple2', {});
a.set('apple3', {});
b.set('apple1', {});
b.set('apple2', {});
b.set('apple3', {});
</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
heavy
a.get('apple1');
a.get('apple2');
a.get('apple3');
pending…
light
b.get('apple1');
b.get('apple2');
b.get('apple3');
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