Parse vs precheck (v2)

Revision 2 of this benchmark created on


Setup

const patterns = [];

for (let i = 0; i < 1900; i++) {
	patterns.push("\"Comments"+i+"\"");
}

for (let i = 0; i < 100; i++) {
	patterns.push("{\"*\":\"{number} comments\",\"_1\":\"1 comment"+i+"\"}");
}

Test runner

Ready to run.

Testing in
TestOps/sec
No check
const result = [];
patterns.forEach((pattern) => {
  try {
	result.push(JSON.parse(pattern));
  } catch {
    result.push(pattern);
  }
});
ready
Yes Check
const result = [];
patterns.forEach((pattern) => {
  if (typeof pattern === 'string' && (pattern[0] === '{' || pattern[0] === '[')) {
    try {
  	  result.push(JSON.parse(pattern));
    } catch {
      result.push(pattern);
    }
  } else {
  	result.push(pattern);
  }
});


ready

Revisions

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