reign
Version:
A persistent, typed-objects implementation.
116 lines (102 loc) • 3.25 kB
JavaScript
;
describeRealm('StringPool', function (options) {
var realm = void 0;
var pool = void 0;
var backing = void 0;
var T = void 0;
before(function () {
realm = options.realm;
backing = realm.backing;
T = realm.T;
pool = realm.strings;
});
describe('Store multiple entries', function () {
var length = 1024;
var input = void 0;
var addresses = void 0;
before(function () {
input = Array.from({ length: length }, function (_, index) {
return 'string ' + index + '.';
});
});
it('should store ' + length + ' unique strings', function () {
addresses = input.map(function (item) {
return pool.add(item);
});
});
it('should have set the reference count to 1 for all the addresses', function () {
addresses.forEach(function (address) {
backing.gc.refCount(address).should.equal(1);
});
});
it('should store the same strings again, returning the same addresses', function () {
input.map(function (item) {
return pool.add(item);
}).forEach(function (address, index) {
address.should.equal(addresses[index]);
});
});
it('should have set the reference count to 2 for all the addresses', function () {
addresses.forEach(function (address) {
backing.gc.refCount(address).should.equal(2);
});
});
it('should have the right length', function () {
addresses.length.should.equal(length);
});
it('should have stored ' + length + ' strings', function () {
input.forEach(function (item) {
pool.has(item).should.equal(true);
});
});
it('should get the address for each item', function () {
input.forEach(function (item, index) {
pool.get(item).should.equal(addresses[index]);
});
});
});
describe('Benchmarks', function () {
var length = 1024;
var asciiInput = void 0,
multiInput = void 0;
var addresses = void 0;
before(function () {
asciiInput = Array.from({ length: length }, function (_, index) {
return 'string ' + index + '.';
});
asciiInput.forEach(function (item) {
return pool.add(item);
});
multiInput = Array.from({ length: length }, function (_, index) {
return '☃☃☃☃☃☃ ' + index + '.';
});
multiInput.forEach(function (item) {
return pool.add(item);
});
});
benchmark('Add strings', 100000, {
ascii: function ascii(index) {
return pool.add('Test ' + index % 50000);
},
multibyte: function multibyte(index) {
return pool.add('☃☃☃☃ ' + index % 50000);
}
});
benchmark('Remove strings', 100000, {
ascii: function ascii(index) {
return pool.remove('Test ' + index % 50000);
},
multibyte: function multibyte(index) {
return pool.remove('☃☃☃☃ ' + index % 50000);
}
});
benchmark('Add pre-existing strings', 100000, {
ascii: function ascii(index) {
return pool.add(asciiInput[index % length]);
},
multibyte: function multibyte(index) {
return pool.add(multiInput[index % length]);
}
});
});
});