Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36
Chrome 90
Mac OS X 10.15.7
Desktop
3 years ago
Test name Executions per second
Baseline encoder 645.3 Ops/sec
baseline hash 1446.3 Ops/sec
Script Preparation code:
AخA
 
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);
Tests:
  • Baseline encoder

     
    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;
    }
  • baseline hash

     
    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;
    }