Test name | Executions per second |
---|---|
Baseline encoder | 645.3 Ops/sec |
baseline hash | 1446.3 Ops/sec |
iterations = 1000;
function randomStringGen(n) {
let strings = [];
for (let i = 0; i < n; i++) {
let s = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
strings[i] = s;
}
return strings;
}
var randomStrings = randomStringGen(iterations);
var encoder = new TextEncoder();
var arrayBuffer = new ArrayBuffer(iterations*1000);
var buffer = new Uint8Array(arrayBuffer);
var j = 0;
for (let i = 0; i < iterations; i++) {
var str = randomStrings[i % iterations];
buffer[j] = str.length;
var res = encoder.encodeInto(str, buffer.subarray(j+1));
j = j + res.written;
}
var stringMap = {};
var counter = 0;
function stringToMap(s) {
var i = stringMap[s];
if (i === undefined) {
stringMap[s] = counter++;
} else {
return i;
}
}
var j = 0;
for (let i = 0; i < iterations; i++) {
var str = randomStrings[i % iterations];
var num = stringToMap(str);
buffer[j] = num;
j = j + 8;
}