A small talk about performance and where the results I'm getting are coming from.
CONTEXT
I'm trying to use a RegExp to get all the matches possible and get a 100 char before and after the match. I use two types of RegExp handling in this case
The 'direct' RegExp looking like that :
myString.match(/[\s\S]{0,100}SomethingImTryingToFind[\s\S]{0,100}/ig);
And the new RegExp looking like that :
myString.match(new RegExp('[\\s\\S]{0,100}SomethingImTryingToFind[\\s\\S]{0,100}', 'ig'))
What's the problem here ?
Now let's say we get some data set on which I wanna use my previous search, I then created two methods that are exactly the same, besides the line written above and look like that.
function getNewRegexpHits(elem) {
var count = 0;
elem.foundKeys = [];
Object.keys(elem).forEach(function(key){
if(typeof elem[key] === 'string') {
elem[key + '_founds'] = (elem[key] || '').match(new RegExp('[\\s\\S]{0,100}e[\\s\\S]{0,100}', 'ig')) || [];
elem[key + '_count'] = elem[key + '_founds'].length;
count += elem[key + '_count'];
if (elem[key + '_count'] > 0) {
elem.foundKeys.push(key);
}
}
});
elem.totalFound = count;
return count;
}
Now I'm just running a basic performance comparison between those two by doing something like that :
results = someFetchingReturningALargeDataSet();
var t0 = performance.now();
results.hits.forEach(function(elem){
getHits(elem);
});
var t1 = performance.now();
console.log("Call to getHits took " + (t1 - t0) + " milliseconds.");
var t2 = performance.now();
results.hits.forEach(function(elem){
getNewRegexpHits(elem);
});
var t3 = performance.now();
console.log("Call to getNewRegexpHits took " + (t3 - t2) + " milliseconds.");
Now both methods work fine and I get the expected results both time but look at the performance issue now .. This is the console output :
Any ideas why that might be ? I actually need to use the new Regexp because I need to use a variable parameter instead of "SomethingImTryingToFind"
Comments
Post a Comment