Improved performance for use replacing with regular expressions

I have an application that currently I replace a string (is not a text fixed, use it a database) on some patterns, I have several replace several expressions that in some cases not made changes since they are not always consistent with expressions, but have them if the data exists. Now I have to update the application since I change more text and a url link, I have thought about changing this part of the code since it seems very novice techniques and now I’m not so much, I’ve improved and learned something more.

I have thought of three possible cases (have not tried new cases), but know which will be the fastest version of run (not is as measure it or prepare a benchmark).

The first stop the current code with several replace (implies change the code every time you add new search and replace terms):

Current and first case:

var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod.';
var date = new Date();

string = string.replace(/EXPR/g, 'NEWVALUE' ).replace(/SECOND_EXPR/g, 'NEWVALUE' ).replace(/THIRD_EXPR/g, 'NEWVALUE' );

The second option, I think it is the best way to maintain the code. An array with all the expressions that must search and replace and go processing one by one:

var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod.';

var date = new Date();

var dataDB = {

    "expr" : "new value",
    "expr" : date,
    "expr" : "new value",
    ...... more properties ......
};

var exprs = [
    {
        search: '#date#',
        change: date
    },{
        search: 'EXPR',
        change: 'NEWVALUE'
    },{
        ....... add 10 more values .......
 ];

exprs.forEach(function(expr){

    string = string.replace(/expr.search/g, dataDB[expr.value] );
});

The third option (adaptation of the above), may be the most fast since it will not process expressions that do not exist in the string, but you will need one larger work, since the database next to the text that changes, should add new fields and enter the expressions to be used, introducing text that will undergo changes in the database together. If only used two regular expressions (defined in the system), two would be stored and only processes these two expressions:

var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod.';

var date = new Date();

var dataDB = {

    "expr" : "new value",
    "expr" : date,
    "expr" : "new value",
    ...... more properties ......
};

var exprsDB = [
    {
        search: '#date#',
        change: date
    },{
        search: 'EXPR',
        change: 'NEWVALUE'
    },{
        ....... only expressions chosen for this text .......
 ];

exprsDB.forEach(function(expr){

    string = string.replace(/expr.search/g, dataDB[expr.value] );
});

Do you option dowerless?. Do you think that there is any better way of doing?. Always work with expressions based on the system, only some texts to implement one or other. If for example has ##date## text, which is replaced by the current date, etc. I’ve been looking on Github if there is already something similar programmed and could use part of the code. It would create functions base with expressions and dataDB to store the name of the function to run? example: ##date## call function changeDate(string) or similar, and return string changed.

Thanks.

Answer

I’ve tried with benchmark.js and it seems that it is more efficient to introduce in the code possible expressions regular (although some patterns are not used) to go into a loop which to be used accurately.

Replace#Normal x 929,265 ops/sec ±6.60% (79 runs sampled)
Replace#forEach x 263,416 ops/sec ±10.30% (64 runs sampled)
Replace#for x 374,252 ops/sec ±9.16% (71 runs sampled)
Fastest is Replace#Normal

Leave a Reply

Your email address will not be published. Required fields are marked *