tokenlist implelemt

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function TokenList() {
        this._map = Object.create(null);
        this._val = ' ';
    }
   
    var proto = TokenList.prototype;
   
    proto.contains = function(token) {
        return token in this._map;
    };
   
    proto.add = function() {
        var m = this._map,
                v = this._val;
        for (var n = 0, l = arguments.length; n < l; ++ n) {
                var token = arguments[n];
                if (!(token in m)) {
                        m[token] = true;
                        v += token + ' ';
                }
        }
        this._val = v;
    };
   
    proto.remove = function() {
        var m = this._map,
                v = this._val;
        for (var n = 0, l = arguments.length; n < l; ++ n) {
                var token = arguments[n];
                delete m[token];
                v = v.replace(' ' + token + ' ', ' ');
        }
        this._val = v;
    };
   
    proto.toString = function() {
        return this._val.slice(1, -1);
    };
   
};
</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
Type 1 - add
var tokens = new TokenList();
for (var n = 0; n < 50; ++n) {
  tokens.add('token' + n);
}

tokens = new TokenList();
for (var n = 0; n < 50; ++n) {
  tokens.add('token' + n);
}
 
pending…
Type 1 - remove
var tokens = new TokenList();
for (var n = 0; n < 50; ++n) {
  tokens.add('token' + n);
}

for (var n = 0; n < 50; ++n) {
  tokens.remove('token' + n);
}
pending…
Type 1 - toString
var tokens = new TokenList();
for (var n = 0; n < 50; ++n) {
  tokens.add('token' + n);
}

for (var n = 0; n < 50; ++n) {
  tokens.toString()
}
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