Skip to main content
added 2 characters in body
Source Link
Thriggle
  • 1.3k
  • 9
  • 23

I do recommend changing the name of your function to something more specific to its process, making it clear that it's looking for a specific type of string. Something like getArrayFromStringCode might be your best bet.

There are several different approaches for parsing strings, but there's no right way. The best way will likely be the one that is easiest to use, maintain, and debug.

My first thought was that a recursive method would be the best way to parse a string for placeholders, so that when you hit an opening bracket you can return the substring up to the closure, then pass the rest of the string back into the recursive function to be processed.

function parseRecursive(code){
     if(code.length >0){
        var nextClosure = code.indexOf("]");
        if(code[0] !== "[" || nextClosure < 0){
        // if this isn't the start of a set or there are no more sets...
        // return an array with this character and keep processing remainder of string
            return code.length > 1 ? 
                [code[0]].concat(parseRecursive(code.substring(1)))
                 : [code[0]]; 
        }else{
        // if this is the start of a set...
        // return an array with the set as a string and keep processing the remainder of the string
            return code.length > nextClosurenextClosure+1 ? 
                [code.substring(1,nextClosure)].concat(parseRecursive(code.substring(nextClosure+1)))
                : [code.substring(1,nextClosure)];
        }
    }
}

This method saw an insignificant performance improvement in Internet Explorer 11, but was actually somewhat slower than your original code in Firefox and Chrome.

My next thought was to use RegEx, but Flambino beat me to the punch! Flambino's method performs significantly faster in Internet Explorer 11, but (alas!) significantly slower in Chrome and Firefox.

Here's a fork of your fiddle with the different approaches.

Here's the jsperf test to see how they compare.

Since recursive programming requires a bit of "special" thinking and regular expressions require a bit of research, the linear approach in your original code may actually be the easiest to maintain in the long run. On the other hand, the regular expression approach has fewer lines and logical tests.

I do recommend changing the name of your function to something more specific to its process, making it clear that it's looking for a specific type of string. Something like getArrayFromStringCode might be your best bet.

There are several different approaches for parsing strings, but there's no right way. The best way will likely be the one that is easiest to use, maintain, and debug.

My first thought was that a recursive method would be the best way to parse a string for placeholders, so that when you hit an opening bracket you can return the substring up to the closure, then pass the rest of the string back into the recursive function to be processed.

function parseRecursive(code){
     if(code.length >0){
        var nextClosure = code.indexOf("]");
        if(code[0] !== "[" || nextClosure < 0){
        // if this isn't the start of a set or there are no more sets...
        // return an array with this character and keep processing remainder of string
            return code.length > 1 ? 
                [code[0]].concat(parseRecursive(code.substring(1)))
                 : [code[0]]; 
        }else{
        // if this is the start of a set...
        // return an array with the set as a string and keep processing the remainder of the string
            return code.length > nextClosure ? 
                [code.substring(1,nextClosure)].concat(parseRecursive(code.substring(nextClosure+1)))
                : [code.substring(1,nextClosure)];
        }
    }
}

This method saw an insignificant performance improvement in Internet Explorer 11, but was actually somewhat slower than your original code in Firefox and Chrome.

My next thought was to use RegEx, but Flambino beat me to the punch! Flambino's method performs significantly faster in Internet Explorer 11, but (alas!) significantly slower in Chrome and Firefox.

Here's a fork of your fiddle with the different approaches.

Here's the jsperf test to see how they compare.

Since recursive programming requires a bit of "special" thinking and regular expressions require a bit of research, the linear approach in your original code may actually be the easiest to maintain in the long run. On the other hand, the regular expression approach has fewer lines and logical tests.

I do recommend changing the name of your function to something more specific to its process, making it clear that it's looking for a specific type of string. Something like getArrayFromStringCode might be your best bet.

There are several different approaches for parsing strings, but there's no right way. The best way will likely be the one that is easiest to use, maintain, and debug.

My first thought was that a recursive method would be the best way to parse a string for placeholders, so that when you hit an opening bracket you can return the substring up to the closure, then pass the rest of the string back into the recursive function to be processed.

function parseRecursive(code){
     if(code.length >0){
        var nextClosure = code.indexOf("]");
        if(code[0] !== "[" || nextClosure < 0){
        // if this isn't the start of a set or there are no more sets...
        // return an array with this character and keep processing remainder of string
            return code.length > 1 ? 
                [code[0]].concat(parseRecursive(code.substring(1)))
                 : [code[0]]; 
        }else{
        // if this is the start of a set...
        // return an array with the set as a string and keep processing the remainder of the string
            return code.length > nextClosure+1 ? 
                [code.substring(1,nextClosure)].concat(parseRecursive(code.substring(nextClosure+1)))
                : [code.substring(1,nextClosure)];
        }
    }
}

This method saw an insignificant performance improvement in Internet Explorer 11, but was actually somewhat slower than your original code in Firefox and Chrome.

My next thought was to use RegEx, but Flambino beat me to the punch! Flambino's method performs significantly faster in Internet Explorer 11, but (alas!) significantly slower in Chrome and Firefox.

Here's a fork of your fiddle with the different approaches.

Here's the jsperf test to see how they compare.

Since recursive programming requires a bit of "special" thinking and regular expressions require a bit of research, the linear approach in your original code may actually be the easiest to maintain in the long run. On the other hand, the regular expression approach has fewer lines and logical tests.

added 8 characters in body
Source Link
Thriggle
  • 1.3k
  • 9
  • 23

I do recommend changing the name of your function to something more specific to its process, making it clear that it's looking for a specific type of string. Something like getArrayFromStringCode might be your best bet.

There are several different approaches for parsing strings, but there's no right way. The best way will likely be the one that is easiest to use, maintain, and debug.

My first thought was that a recursive method would be the best way to parse a string for placeholders, so that when you hit an opening bracket you can return the substring up to the closure, then pass the rest of the string back into the recursive function to be processed.

function parseRecursive(code){
     if(code.length >0){
        var nextClosure = code.indexOf("]");
        if(code[0] !== "[" || nextClosure < 0){
        // if this isn't the start of a set or there are no more sets...
        // return an array with this character and keep processing remainder of string
            return code.length > 1 ? 
                [code[0]].concat(parseRecursive(code.substring(1)))
                 : [code[0]]; 
        }else{
        // if this is the start of a set...
        // return an array with the set as a string and keep processing the remainder of the string
            return code.length > nextClosure ? 
                [code.substring(1,nextClosure)].concat(parseRecursive(code.substring(nextClosure+1)))
                : [code.substring(1,nextClosure)];
        }
    }
}

This method saw a modestan insignificant performance improvement in Internet Explorer 11, but was actually somewhat slower than your original code in Firefox and Chrome.

My next thought was to use RegEx, but Flambino beat me to the punch! Flambino's method performs significantly faster in Internet Explorer 11, but (alas!) significantly slower in Chrome and Firefox.

Here's a fork of your fiddle with the different approaches.

Here's the jsperf test to see how they compare.

Since recursive programming requires a bit of "special" thinking and regular expressions require a bit of research, the linear approach in your original code may actually be the easiest to maintain in the long run. On the other hand, the regular expression approach has fewer lines and logical tests.

I do recommend changing the name of your function to something more specific to its process, making it clear that it's looking for a specific type of string. Something like getArrayFromStringCode might be your best bet.

There are several different approaches for parsing strings, but there's no right way. The best way will likely be the one that is easiest to use, maintain, and debug.

My first thought was that a recursive method would be the best way to parse a string for placeholders, so that when you hit an opening bracket you can return the substring up to the closure, then pass the rest of the string back into the recursive function to be processed.

function parseRecursive(code){
     if(code.length >0){
        var nextClosure = code.indexOf("]");
        if(code[0] !== "[" || nextClosure < 0){
        // if this isn't the start of a set or there are no more sets...
        // return an array with this character and keep processing remainder of string
            return code.length > 1 ? 
                [code[0]].concat(parseRecursive(code.substring(1)))
                 : [code[0]]; 
        }else{
        // if this is the start of a set...
        // return an array with the set as a string and keep processing the remainder of the string
            return code.length > nextClosure ? 
                [code.substring(1,nextClosure)].concat(parseRecursive(code.substring(nextClosure+1)))
                : [code.substring(1,nextClosure)];
        }
    }
}

This method saw a modest performance improvement in Internet Explorer 11, but was actually somewhat slower than your original code in Firefox and Chrome.

My next thought was to use RegEx, but Flambino beat me to the punch! Flambino's method performs significantly faster in Internet Explorer 11, but (alas!) significantly slower in Chrome and Firefox.

Here's a fork of your fiddle with the different approaches.

Here's the jsperf test to see how they compare.

Since recursive programming requires a bit of "special" thinking and regular expressions require a bit of research, the linear approach in your original code may actually be the easiest to maintain in the long run. On the other hand, the regular expression approach has fewer lines and logical tests.

I do recommend changing the name of your function to something more specific to its process, making it clear that it's looking for a specific type of string. Something like getArrayFromStringCode might be your best bet.

There are several different approaches for parsing strings, but there's no right way. The best way will likely be the one that is easiest to use, maintain, and debug.

My first thought was that a recursive method would be the best way to parse a string for placeholders, so that when you hit an opening bracket you can return the substring up to the closure, then pass the rest of the string back into the recursive function to be processed.

function parseRecursive(code){
     if(code.length >0){
        var nextClosure = code.indexOf("]");
        if(code[0] !== "[" || nextClosure < 0){
        // if this isn't the start of a set or there are no more sets...
        // return an array with this character and keep processing remainder of string
            return code.length > 1 ? 
                [code[0]].concat(parseRecursive(code.substring(1)))
                 : [code[0]]; 
        }else{
        // if this is the start of a set...
        // return an array with the set as a string and keep processing the remainder of the string
            return code.length > nextClosure ? 
                [code.substring(1,nextClosure)].concat(parseRecursive(code.substring(nextClosure+1)))
                : [code.substring(1,nextClosure)];
        }
    }
}

This method saw an insignificant performance improvement in Internet Explorer 11, but was actually somewhat slower than your original code in Firefox and Chrome.

My next thought was to use RegEx, but Flambino beat me to the punch! Flambino's method performs significantly faster in Internet Explorer 11, but (alas!) significantly slower in Chrome and Firefox.

Here's a fork of your fiddle with the different approaches.

Here's the jsperf test to see how they compare.

Since recursive programming requires a bit of "special" thinking and regular expressions require a bit of research, the linear approach in your original code may actually be the easiest to maintain in the long run. On the other hand, the regular expression approach has fewer lines and logical tests.

Source Link
Thriggle
  • 1.3k
  • 9
  • 23

I do recommend changing the name of your function to something more specific to its process, making it clear that it's looking for a specific type of string. Something like getArrayFromStringCode might be your best bet.

There are several different approaches for parsing strings, but there's no right way. The best way will likely be the one that is easiest to use, maintain, and debug.

My first thought was that a recursive method would be the best way to parse a string for placeholders, so that when you hit an opening bracket you can return the substring up to the closure, then pass the rest of the string back into the recursive function to be processed.

function parseRecursive(code){
     if(code.length >0){
        var nextClosure = code.indexOf("]");
        if(code[0] !== "[" || nextClosure < 0){
        // if this isn't the start of a set or there are no more sets...
        // return an array with this character and keep processing remainder of string
            return code.length > 1 ? 
                [code[0]].concat(parseRecursive(code.substring(1)))
                 : [code[0]]; 
        }else{
        // if this is the start of a set...
        // return an array with the set as a string and keep processing the remainder of the string
            return code.length > nextClosure ? 
                [code.substring(1,nextClosure)].concat(parseRecursive(code.substring(nextClosure+1)))
                : [code.substring(1,nextClosure)];
        }
    }
}

This method saw a modest performance improvement in Internet Explorer 11, but was actually somewhat slower than your original code in Firefox and Chrome.

My next thought was to use RegEx, but Flambino beat me to the punch! Flambino's method performs significantly faster in Internet Explorer 11, but (alas!) significantly slower in Chrome and Firefox.

Here's a fork of your fiddle with the different approaches.

Here's the jsperf test to see how they compare.

Since recursive programming requires a bit of "special" thinking and regular expressions require a bit of research, the linear approach in your original code may actually be the easiest to maintain in the long run. On the other hand, the regular expression approach has fewer lines and logical tests.