Compare speed of checking that a character is a whitespace

Benchmark created on


Setup

const spaces = ' \f\n\r\t\v\u00A0\u2028\u2029';
const spaceSet = new Set(spaces);

function isSpaceSet(char) {
  return spaceSet.has(char);
}

function isSpaceChar(char) {
  return spaces.includes(char);
}

function isSpaceCharCode(char) {
  const charCode = char.charCodeAt(0);
  return ((charCode <= 160 && (charCode >= 9 && charCode <= 13) || charCode == 32 || charCode == 160) || charCode == 5760 || charCode == 6158 ||
    (charCode >= 8192 && (charCode <= 8202 || charCode == 8232 || charCode == 8233 || charCode == 8239 || charCode == 8287 || charCode == 12288 || charCode == 65279)));
}

Test runner

Ready to run.

Testing in
TestOps/sec
Use charCode comparison
isSpaceCharCode('a');
isSpaceCharCode(' ');
isSpaceCharCode('1');
isSpaceCharCode('\t');

ready
Use String.includes
isSpaceChar('a');
isSpaceChar(' ');
isSpaceChar('1');
isSpaceChar('\t');
ready
Use Set.has
isSpaceSet('a');
isSpaceSet(' ');
isSpaceSet('1');
isSpaceSet('\t');
ready

Revisions

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