dictionary asjdflkjasd (v4)

Revision 4 of this benchmark created on


Preparation HTML

<script>
function Dictionary(kvPairs) {
        kvPairs = kvPairs === undefined ? [] : kvPairs;
        var kvPairsLength = kvPairs.length;
        // instance variables
        this.size = 0;
        this._keyIndex = {};
        this._keys = [];
        this._values = [];
        this._tailIndex = 0;
        this._isCompact = true;
        // methods
        this.put = put;
        this.puts = puts;
        this.get = get;
        this.gets = gets;
        this.remove = remove;
        this.keys = keys;
        this.values = values;
        this.keyValuePairs = keyValuePairs;
        this.forEachKey = forEachKey;
        this.forEachValue = forEachValue;
        this.forEachKeyValue = forEachKeyValue;
        this._compact = compact;
        // initialization
        this.puts(kvPairs);
    }
    function put(key, value) {
        var keyIndex = this._keyIndex;
        var index = keyIndex[key];
        if (index === undefined) {
            index = this._tailIndex++;
            keyIndex[key] = index;
            ++this.size;
        }
        this._keys[index] = key;
        this._values[index] = value;
    }
    function puts(kvPairs) {
        var kvPairsLength = kvPairs.length;
        for (var i = 0; i < kvPairsLength; ++i) {
            var kvPair = kvPairs[i];
            this.put(kvPair[0], kvPair[1]);
        }
    }
    function get(key) {
        return this._values[this._keyIndex[key]];
    }
    function gets(keys) {
        var keysLength = keys.length;
        var result = new Array(keysLength);
        for (var i = 0; i < keysLength; ++i) {
            result[i] = this._values[this._keyIndex[keys[i]]];
        }
        return result;
    }
    // does not yet resize arrays
    function remove(key) {
        var keyIndex = this._keyIndex;
        var index = keyIndex[key];
        if (index !== undefined) {
            keyIndex[key] = undefined;
            this._keys[index] = undefined;
            this._values[index] = undefined;
            --this.size;
            this._isCompact = false;
        }
    }
    function keys() {
        if (!this._isCompact) {
            this._compact();
            this._isCompact = true;
        }
        return this._keys.slice();
    }
    function values() {
        if (!this._isCompact) {
            this._compact();
            this._isCompact = true;
        }
        return this._values.slice();
    }
    function compact() {
        var keys = this._keys;
        var values = this._values;
        var index = keys.indexOf(undefined);
        console.log(index);
        while (index !== -1) {
            keys.splice(index, 1);
            values.splice(index, 1);
            index = keys.indexOf(undefined, index);
            --this._tailIndex;
        }
    }
    function keyValuePairs() {
        if (!this._isCompact) {
            this._compact();
            this._isCompact = true;
        }
        var keys = this._keys;
        var values = this._values;
        var keysLength = keys.length;
        var result = new Array(keysLength);
        for (var i = 0; i < keysLength; ++i) {
            result[i] = [keys[i], values[i]];
        }
        return result;
    }
    function forEachKey(callback) {
        forEach(this.keys(), callback);
    }
    function forEachValue(callback) {
        forEach(this.values(), callback);
    }
    function forEach(array, callback) {
        var arrayLength = array.length;
        for (var i = 0; i < arrayLength; ++i) {
            callback(array[i]);
        }
    }
    function forEachKeyValue(callback) {
        var kvPairs = this.keyValuePairs();
        var kvPairsLength = kvPairs.length;
        for (var i = 0; i < kvPairsLength; ++i) {
            var pair = kvPairs[i];
            callback(pair[0], pair[1]);
        }
    }
</script>

Setup

var a = new Array(1000);
    for (var i = 0; i < a.length; ++i) {
      a[i] = [i, i];
    }
    var d = new Dictionary(a);

Test runner

Ready to run.

Testing in
TestOps/sec
1
d.keys();
d.values();
ready
2
d.keyValuePairs()
ready

Revisions

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