Parse vs precheck

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+"\"}");
}

console.log(patterns);

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);
  }
});
console.log(result.length);
ready
Yes One check Check
const result = [];
patterns.forEach((pattern) => {
  if (typeof table === 'string') {
    try {
  	  result.push(JSON.parse(pattern));
    } catch {
      // noop
    }
  } else {
  	result.push(pattern);
  }
});
console.log(result.length);


ready
Yes Two check Check
const result = [];
patterns.forEach((pattern) => {
  if (typeof table === 'string' && pattern[0] === '{') {
    try {
  	  result.push(JSON.parse(pattern));
    } catch {
      // noop
    }
  } else {
  	result.push(pattern);
  }
});
console.log(result.length);
ready
Yes Three check Check
const result = [];
patterns.forEach((pattern) => {
  if (typeof table === 'string' && (pattern[0] === '{' || pattern[0] === '[')) {
    try {
  	  result.push(JSON.parse(pattern));
    } catch {
      // noop
    }
  } else {
  	result.push(pattern);
  }
});
console.log(result.length);
ready

Revisions

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