Substring vs JSON.parse

Benchmark created on


Description

Getting property value from a stringified object

Test runner

Ready to run.

Testing in
TestOps/sec
Substring
function getValueOfPropFromStringifiedJSON(stringifiedJSON, prop) {
  // Using substring
  if (!stringifiedJSON || !prop) return null;
  const propIndex = stringifiedJSON.indexOf(prop);
  const valueIndex = stringifiedJSON.indexOf(":", propIndex);
  const value = stringifiedJSON.substring(valueIndex + 1, stringifiedJSON.indexOf(",", valueIndex));
  return value;
}

const DATA = '{"u":1,"t":150,"p":100}';
const valueStr = getValueOfPropFromStringifiedJSON(DATA, "t");
let covertedToInt = parseInt(valueStr, 10);
ready
JSON.parse
const DATA = '{"u":1,"t":150,"p":100}';
const parsedData = JSON.parse(DATA);
valueTickerId = parsedData ? parseInt(parsedData.t, 10) : undefined;
ready

Revisions

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