Dynamic vs generated

Benchmark created on


Description

Love the jit

Setup

// as mentioned real code would not go through strings

const header = "Name,Age\n";
const content = "Benjamin,35\nDaniel,2\nLeo,0\n".repeat(200000).trim();

const csv = header + content

Test runner

Ready to run.

Testing in
TestOps/sec
parse with allocations generated by copilot
function parse(csv) {
  // Shouldn't go through strings and should keep buffer
  // but ignore for example
  let [header, ...lines] = csv.split("\n");
  let headerValues = header.split(",");
  const result = [];
  for(const row of lines) {
    let obj = {};
    let values = row.split(",");
    for(const [index, value] of values.entries()) {
      obj[headerValues[index]] = value;
    }
    result.push(obj);
  }
  return result;
}


if(parse(csv).length !== 600000) {
  throw 'up';
}
ready
dynamic recompilation
function parse(csv) {
  // Shouldn't go through strings and should keep buffer
  // but ignore for example
  let lines = csv.trim().split("\n");
  let headerValues = lines[0].split(",");
  
  const parser = Function("return {" + headerValues.map((value, index) => `${value}: arguments[${index}]`).join(",") + "}");
  
  const result = Array(lines.length - 1);
  for(let i = 1; i < lines.length; i++) {
      result[i - 1] = parser.apply(null, lines[i].split(","));
  }
  return result
}

if(parse(csv).length !== 600000) {
  throw 'up';
}
ready

Revisions

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