Simple matching vs vector classification

Benchmark publishedon

Setup

let html_as_string = `
<html>
<body>
<div><div></div><div></div><div></div><div></div></div>
</body>
</html>
`

let html_as_ascii = [...html_as_string].map(char => char.charCodeAt(0));



Test Runner

Initializing...

Testing in
Test CaseOps/sec
Basic
function checkChar(char) {
  /* The more symbols to check against the worse this gets. */
  return char == 0x0D /* '\r' */ || 
      char == 0x26 /* '&' */  || 
      char == 0x27 /* ... */  || 
      char == 0x28 /* ... */  ||  
      char == 0x29 /* ... */  || 
      char == 0x2A /* ... */  ||
      char == 0x01 /* ... */  ||
      char == 0x02 /* ... */  ||
      char == 0x03 /* ... */  ||
      char == 0x04 /* ... */  ||
      char == 0x05 /* ... */  ||
      char == 0x3C /* < */  ||
      char == 0x00 /*'\0' */;
}

let count = 0;
for (let i = 0; i < html_as_ascii.length; i++) {
  if (checkChar(html_as_ascii[i]))
    count++;
}
ready
Table lookup
const lower_table  = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0];

const higher_table  = [0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0];

function checkChar(char) {
  const low_bits = char & 0xf;
  const high_bits = char >> 4;
  /* With SIMD the tables do not necessarily exist in global memory. */
  return lower_table[low_bits] & higher_table[high_bits];
}

let count = 0;
for (let i = 0; i < html_as_ascii.length; i++) {
  if (checkChar(html_as_ascii[i]))
    count++;
}
ready

Revisions

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

Revision 1
publishedon