patternjavascriptMinor
Parsing a string to an object
Viewed 0 times
objectstringparsing
Problem
I have this string containing this block of code:
Currently I use regex to read this into an object like this:
This gives me an object like this:
The
[General]
StartWithLastProfile=0
Profilist.notifications=true
Profilist.dev=true
Profilist.dev-builds=80k
Profilist.launch_on_create=true
[Profile0]
Name=Dev Profilist
IsRelative=1
Path=Profiles/j0a1zjle.Unnamed Profile 1
Profilist.tie=0
[Profile55]
Name=Main
IsRelative=1
Path=Profiles/qekfxcdm.Unnamed Profile 1Currently I use regex to read this into an object like this:
var str = '[General]\nStartWithLastProfile=0\nProfilist.notifications=true\nProfilist.dev=true\nProfilist.dev-builds=80k\nProfilist.launch_on_create=true\n\n[Profile0]\nName=Dev Profilist\nIsRelative=1\nPath=Profiles/j0a1zjle.Unnamed Profile 1\nProfilist.tie=0\n\n[Profile55]\nName=Main\nIsRelative=1\nPath=Profiles/qekfxcdm.Unnamed Profile 1';
console.time('parse');
var linePatt = /^(?:\[(.*)\]|(.*?)=(.*))$/gm;
var match;
var objParsed = {};
var lastKey;
while(match = linePatt.exec(str)) {
console.info(match);
// Array [ "[General]", "General", undefined, undefined ]
// Array [ "StartWithLastProfile=0", undefined, "StartWithLastProfile", "0" ]
if (match[2] == undefined) {
lastKey = match[1];
objParsed[lastKey] = {};
} else {
objParsed[lastKey][match[2]] = match[3];
}
}
console.timeEnd('parse');
console.log('objParsed:', JSON.stringify(objParsed));This gives me an object like this:
{
"General": {
"StartWithLastProfile": "0",
"Profilist.notifications": "true",
"Profilist.dev": "true",
"Profilist.dev-builds": "80k",
"Profilist.launch_on_create": "true"
},
"Profile0": {
"Name": "Dev Profilist",
"IsRelative": "1",
"Path": "Profiles/j0a1zjle.Unnamed Profile 1",
"Profilist.tie": "0"
},
"Profile55": {
"Name": "Main",
"IsRelative": "1",
"Path": "Profiles/qekfxcdm.Unnamed Profile 1"
}
}The
console.time and console.timeEnd tell me this takes on average from 4-7ms. My application is Solution
I believe a simple split on newline and a hard-coded parse would be better than the regex solution.... especially if you can trust your input data will be valid. In essence, the grouping and iterative looping in the regex is probably the slow part.
Of course, the
Let me explain the hard-parse with a code example:
See the comparative results in this jsfiddle
Of course, the
console.info for each loop is also very slow.... and probably is something you added afterwards? If it is in your actual code, that's almost certainly the problem.Let me explain the hard-parse with a code example:
var objParsed = {};
var lastKey;
var lines = str.split('\n');
for(var i = 0; i < lines.length; i++) {
var clause = lines[i].trim();
if (clause.length == 0) {
continue;
}
if (clause[0] == '[') {
lastKey = {};
objParsed[clause.substring(1, clause.length - 1)] = lastKey;
} else {
var pos = clause.indexOf("=");
lastKey[clause.substring(0, pos)] = clause.substring(pos + 1);
}
}See the comparative results in this jsfiddle
Code Snippets
var objParsed = {};
var lastKey;
var lines = str.split('\n');
for(var i = 0; i < lines.length; i++) {
var clause = lines[i].trim();
if (clause.length == 0) {
continue;
}
if (clause[0] == '[') {
lastKey = {};
objParsed[clause.substring(1, clause.length - 1)] = lastKey;
} else {
var pos = clause.indexOf("=");
lastKey[clause.substring(0, pos)] = clause.substring(pos + 1);
}
}Context
StackExchange Code Review Q#93093, answer score: 3
Revisions (0)
No revisions yet.