Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ITFB. Reinventing JSON parser
(version: 0)
Writing our own json parser for internal purposes only
Comparing performance of:
eval vs ES4 (old JS, Siebel eScript) vs ES6 (new JS)
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var jsObj = { boolY: true, boolX: false, str: 'string', num: 123, obj: {}, arr: [{ name: 'Alex', type: 'manager' }, { name: 'Bob', type: 'ceo' }], undef: undefined, null: null, }; var json = JSON.stringify(jsObj, null, 2); console.log({ json: json }); function parseJSON_eval(json) { return eval(`(${json})`); } function parseJSON_ES4(json) { // Higher-order function to be used for detecting type var firstAndLastChars = function firstAndLastChars(first, last) { return function(str) { return str[0] === first && str[str.length - 1] === last; }; }; var isArray = firstAndLastChars('[', ']'); var isObj = firstAndLastChars('{', '}'); var hasDoubleQuotes = firstAndLastChars('"', '"'); var hasSingleQuotes = firstAndLastChars("'", "'"); var isNumber = function isNumber(str) { return +str + '' === str; }; var isString = function isString(str) { str = str.trim(); return (hasSingleQuotes(str) || hasDoubleQuotes(str)) && str[str.length - 2] !== '\\'; }; var removeFirstAndLastChar = function removeFirstAndLastChar(str) { str = str.trim(); return str.substring(1).slice(0, str.length - 2) || ''; }; // Higher-order function to be used for splitting string var splitByChar = function splitByChar(base_char) { return function(str) { var result = []; var double_string_open = false; var single_string_open = false; var array_open = false; var object_open = false; var array_bracket_count = 0; var object_bracket_count = 0; var curr_str = ''; var prev_ch = ''; for (var i = 0; i < str.length; i += 1) { var ch = str[i]; if (ch === '"') { double_string_open = !double_string_open; } if (ch === "'") { single_string_open = !single_string_open; } if (ch === '[') { array_bracket_count += 1; array_open = true; } if (ch === ']') { array_bracket_count -= 1; if (array_bracket_count === 0) { array_open = false; } } if (ch === '{') { object_bracket_count += 1; object_open = true; } if (ch === '}') { object_bracket_count -= 1; if (object_bracket_count === 0) { object_open = false; } } if (ch === base_char && !double_string_open && !single_string_open && !array_open && !object_open) { if (curr_str !== '') result.push(curr_str.trim()); curr_str = ''; prev_ch = ''; } else { curr_str += ch; prev_ch = ch; } } if (curr_str !== '') result.push(curr_str.trim()); return result; }; }; var separeateStringByCommas = splitByChar(','); var separeateStringByColons = splitByChar(':'); var parseJSONString = function parseJSONString(str, parent) { str = str.trim(); if (isArray(str)) return separeateStringByCommas(removeFirstAndLastChar(str)).map(parseJSONString); if (isObj(str)) { var obj = {}; var _obj = separeateStringByCommas(removeFirstAndLastChar(str)); // _obj is an array of strings with 'key: value' _obj.forEach(function(val, i) { var key_val_pair = separeateStringByColons(val); // split into key, value if (key_val_pair.length === 2) { obj[parseJSONString(key_val_pair[0])] = parseJSONString(key_val_pair[1]); } }); return obj; } if (isString(str)) return removeFirstAndLastChar(str).replace(/([\\]{1})([\\\"]{1})/g, '$2'); if (isNumber(str)) return +str; if (str === 'null') return null; if (str === 'false') return false; if (str === 'true') return true; if (str === 'undefined') return undefined; throw new SyntaxError('Unexpected end of input'); }; return parseJSONString(json); } function parseJSON_ES6(json) { // Higher-order function to be used for detecting type const firstAndLastChars = function(first, last) { return str => str[0] === first && str[str.length - 1] === last; }; const isArray = firstAndLastChars('[', ']'); const isObj = firstAndLastChars('{', '}'); const hasDoubleQuotes = firstAndLastChars('"', '"'); const hasSingleQuotes = firstAndLastChars("'", "'"); const isNumber = str => +str + '' === str; const isString = function(str) { str = str.trim(); return (hasSingleQuotes(str) || hasDoubleQuotes(str)) && str[str.length - 2] !== '\\'; }; const removeFirstAndLastChar = function(str) { str = str.trim(); return str.substring(1).slice(0, str.length - 2) || ''; }; // Higher-order function to be used for splitting string const splitByChar = function(base_char) { return function(str) { var result = []; var double_string_open = false; var single_string_open = false; var array_open = false; var object_open = false; var array_bracket_count = 0; var object_bracket_count = 0; var curr_str = ''; var prev_ch = ''; for (var i = 0; i < str.length; i += 1) { var ch = str[i]; if (ch === '"') { double_string_open = !double_string_open; } if (ch === "'") { single_string_open = !single_string_open; } if (ch === '[') { array_bracket_count += 1; array_open = true; } if (ch === ']') { array_bracket_count -= 1; if (array_bracket_count === 0) { array_open = false; } } if (ch === '{') { object_bracket_count += 1; object_open = true; } if (ch === '}') { object_bracket_count -= 1; if (object_bracket_count === 0) { object_open = false; } } if (ch === base_char && !double_string_open && !single_string_open && !array_open && !object_open) { if (curr_str !== '') result.push(curr_str.trim()); curr_str = ''; prev_ch = ''; } else { curr_str += ch; prev_ch = ch; } } if (curr_str !== '') result.push(curr_str.trim()); return result; }; }; const separeateStringByCommas = splitByChar(','); const separeateStringByColons = splitByChar(':'); const parseJSONString = function(str, parent) { str = str.trim(); if (isArray(str)) return separeateStringByCommas(removeFirstAndLastChar(str)).map(parseJSONString); if (isObj(str)) { var obj = {}; var _obj = separeateStringByCommas(removeFirstAndLastChar(str)); // _obj is an array of strings with 'key: value' _obj.forEach(function(val, i) { var key_val_pair = separeateStringByColons(val); // split into key, value if (key_val_pair.length === 2) { obj[parseJSONString(key_val_pair[0])] = parseJSONString(key_val_pair[1]); } }); return obj; } if (isString(str)) return removeFirstAndLastChar(str).replace(/([\\]{1})([\\\"]{1})/g, '$2'); if (isNumber(str)) return +str; if (str === 'null') return null; if (str === 'false') return false; if (str === 'true') return true; if (str === 'undefined') return undefined; throw new SyntaxError('Unexpected end of input'); }; return parseJSONString(json); }
Tests:
eval
parseJSON_eval(json)
ES4 (old JS, Siebel eScript)
parseJSON_ES4(json)
ES6 (new JS)
parseJSON_ES6(json)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
ES4 (old JS, Siebel eScript)
ES6 (new JS)
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
It looks like we have a large code snippet and some benchmark results! After analyzing the code snippet, I notice that it appears to be a custom parser for JSON strings, written in JavaScript. The `parseJSON` function takes a JSON string as input and returns an object representing the parsed JSON data. The benchmark results seem to compare the performance of this custom parser against two different JavaScript engines: ES4 (old JS) and ES6 (new JS). The results are likely measured by the number of executions per second for each test case. To provide a helpful answer, I'll need more context or specific questions about the code snippet or benchmark results. However, here's a possible direction to take: 1. Are you experiencing performance issues with this custom parser in your application? 2. Do you want to optimize the parser for better performance? 3. Are there any specific features or errors that you'd like me to focus on? If you have more questions or need further assistance, feel free to ask!
Related benchmarks:
json parse vs eval
json parse vs eval - version 2
eval vs json parse only, no var declaration
eval vs json parse only, no var declaration (identical input)
Comments
Confirm delete:
Do you really want to delete benchmark?