3
\$\begingroup\$

I have this string containing this block of code:

[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

Currently 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 for a much bigger file, though, and goes from Profile0 to ProfileN the blocks repeat. Is there any faster way you would do this?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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 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

\$\endgroup\$
1
  • \$\begingroup\$ Thanks @roflfl I tested this and it seems to take .5ms on average. I retested the regex above without the console.log and it took .38ms on average. Weird stuff. \$\endgroup\$
    – Noitidart
    Commented Jun 9, 2015 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.