RegExp test vs raw string op

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var asSentence = ['They found their friend ill.', 'She was leaving defeated.', 'He stood there, his hands in his pockets.', 'Julia, being a nun, spent much of her time in prayer and meditation.', 'He won the last election with a program designed as the negation of the former program.', 'John heard Mary crying/John heard Mary cry.', 'He went out, an ugly old man.', 'The old man came in a shabby coat.', 'She was quick to shut the door.', 'I bought a belt for CZK 70.', 'He repaid his debts to the insurance company.', 'He arrived on Thursday 5th (of) January 1997.', 'It is located two kilometers from the river.', 'We will meet at the Central station in the hall.', 'Give it to our director Novak.', 'There are steamboats on the Vltava river.', 'Your performance: miserable.', 'In Praha, at five o\'clock.', 'An important event.', 'George!', 'Sorry.', 'John Smith, London.', 'A book review: The Hours is a new book.', 'Ooops, I didn\'t mean that.', 'Sir, I\'m not staying any longer.', 'You have two possibilities how to get money.', 'The professor, inspired by the new article, gave a lecture on the new issues.', 'Judging by his face, he was angry.', 'Frankly speaking, she has chosen a wrong partner.', 'He has always been an influential, though controversial figure.', 'He run into an elephant in the garden, which killed him.', 'He left, never to come back again.', 'If I seem angry sometimes, it\'s usually because I\'m very tired.', 'She had not arrived, which made him sad.', 'We have not answered your question completely, for which we apologize.', 'He\'s got five points, which was the maximum.', 'Eleanor\'s pen, which had been lying on the table, suddenly moved.', 'Guidance on the Circumstances, in which the Parents may Choose to Educate their Children at Home.', 'She\'s poor, that\'s why she had to come to Arthur.', 'In the process, the uranium (loses, or) is depleted(,) of almost half its radioactivity, which is how depleted uranium gets its name.', 'Another thing the microscope revealed was the difference between forming a laminate using vacuum pressure, which is how 3DL is made, and forming a laminate using pressure from rollers, which is how (a few of) the typical (‘look-alike’) sails are made.', 'This is where you find Ogden Utah, which is where the Central Pacific and Union Pacific railroads met in 1869.', 'We have reached the point where we want to sum up an infinite number of differential amounts, which is when we integrate.', 'To be successful, the formula is this reach combined with frequency, which is how often viewers will receive.', 'N is for never, which is how often he\'s sincere when he says he cares.', 'He was troubled by insects etc.', 'I saw and heard Mary sing.', 'Peter had been working on his dissertation and preparing for an English exam the whole day but in the evening he was doing nothing more.', 'John was stressed and difficult to get along with.', 'Peter arrived and Paul probably as well.', 'The match ended in a 5:0 draw.', 'In the ears 1995-1999 I was attending the high school.', 'Everybody was watching, from kids to adults.', 'Peter (and Paul) came there.', 'You can (if you dare) follow John.', 'The team ended up third (last year they lied first).', 'To think we might have been in Rome now!', 'The court, as it seems to me, has no opinion on the subject.', 'Mary prepared the lunch and John the dinner.', 'Why me?', 'The blue slippers are torn, the green dirty.', 'Only the younger came.', '(She did not leave.) Because she wouldn\'t be there in time.', 'He bought red and white wine.', 'The presidents of Greece and of Austria were present at the ceremony.', '(Has the shop assistant wrapped the book?) He has.', '(The company was supposed to deliver the goods to the customer.) The delivery did not take place.', 'The boss has left.', 'Houses are built from bricks.', 'It was announced on the radio.', 'John and Mary kissed (each other).', 'Mary sang like John.', 'Mary sang like John did.', 'He is fit as a fiddle.', 'Mary is the same as John.', 'She does nothing but complain all day long.', 'Except for dates, all ordinals should be written in words.', 'Except this week I\'ll be teaching regularly.', '(Lane closures will effect Castle Boulevard and Castle bridge Road the 5th February up to the 25th March), access is maintained everywhere apart from to the Castle.', 'Reformists have no access to free media except to the Internet.', 'We do not share email addresses with third parties unless required to do so by law.', 'Apart from the fact that she sings well she can do nothing.', 'I never take a bath but the phone rings or someone knocks the door.', 'But for John, we would have lost this match.', 'Barring accidents, we\'ll be there on time.', 'He did all but strangled me.', 'I would have told them the truth, except/except that they wouldn\'t have believed me.', 'Besides to Rome, they also wanted to go to Venice.', 'Herdwicks (start lambing a year after other breeds because they) need to grow enough.EXT to stand.RESL the harsh climate.', 'People with AIDS have enough to bear.', 'Peter didn\'t park in the parking lot.', 'Not only is the budget not in surplus, but it is even covertly deficit.', 'I will start to work on Monday.', 'Peter can come to the concert./Peter will come to the concert.', 'Peter cannot come and stay./Peter will not come and stay.', 'Peter could not, and cannot, come to the concert./Peter should not, and will not, come to the concert.', 'So they could not and cannot confirm or deny it./So they should not and will not confirm or deny it.', 'He had the right to protest.', 'She made a decision.', 'Petr got the order to come.', 'Pavel made Petr a suggestion.', 'The cat is a mammal.', 'Out of the blue they appeared in our room.', 'I\'m all ears.', 'I\'ve got five houses and three cars.', 'They elected three out of five vice-chairs.', 'Billions of people live here.', 'The new Golf 500 is already on the market.', 'We only won twice.', 'We have missed the third.', 'The United Nations Organization for Education, Science and Culture.', 'His Hamlet is all torn.', 'Guess Who\'s Coming to Dinner is fun.', 'Kerouac\'s On the Road', 'the company Sady, Lesy, Zahradnictví', 'He was contributing to the Swedish Svenska Dagbladet.', 'He said he wouldn\'t come: "I will certainly not arrive".', 'The coach: "It was not too bad."', 'His words "I envy him" were surprising.', 'He pointed at me: "You".', 'The mood is getting better: "We will beat them".', 'It took as long as to 1989 until the decision was taken to open the Journal to contributions from all over the world.', 'They passed on to customers as much as 50% of the costs of buying out their broken contracts.', 'If this harvest is good, then they will not need to import wheat.', 'Janice has been together with John four years now.', 'Sources close to the CEO say he is ready to make a deal.', 'But maintaining U.S. influence will be difficult in the face of Japanese dominance in the region.', 'Her son was happily playing at the family home in contradiction of a prognosis 12 years ago that he was not \'worth treating\'.', 'He last sailed upon it in charge of a steamer\'s course.', 'John seems to understand.', 'John expects Mary to leave.', 'John hears Mary cry.', 'Why bother?', 'It\'s been kind of annoying, to say the least.', 'To think we might have been in Rome now!', 'To leave early would be impolite.', 'He refused to cooperate.', 'I was invited to contribute to the volume.', 'John seems to understand.', 'I am to blame.', 'It is to be stressed that Peter was living in London in 1976.', 'They made him stay.', 'John is eager to please.', 'John is difficult to please/It is difficult to please John.', 'This flat must have cost a lot to furnish.', 'She was quick to shut the door.', 'She was likely to leave.', 'She was lucky to get that job.', 'Sue made the decision to have a staff-directory in the phonebook.', 'She did not have the nerve to leave.', 'They had the votes necessary to defeat the amendment.', 'He stopped to have a drink.', 'The firms are moving faster to create new adds than they did in the fall of 1987.', 'She has gone to see her sister.', 'I\'m too young to marry.', 'The weather was not such as to favour long walking hours.', 'He was rich enough to maintain a fleet.', 'I don\'t know her to speak to.', 'The first player to score', 'The most convenient time to go is Saturday.', 'He has a large family to provide for.', 'the problem of what to offer in return', 'There is nothing to be seen.', 'There are so few horses to see.', 'His leaving no address was most inconvenient.', 'A close friend of Geremew Ketema reported his being killed to OSG on 17 June.', 'I hate killing animals.', 'I\'m afraid of antagonizing him.', 'You were fortunate in winning his support.', 'More dual-carreer couples are also getting help, with men increasingly bringing their wives for joint counselling.', 'It is quicker than going by train.', 'Having delimited the field of inquiry, we shall concentrate on the problem...', 'There will be singing.', 'There was no stopping him.', 'There will be people dancing.', 'It\'s no good bothering about it.', 'It is feared that the ship was wrecked.', 'He made it his business to secure financial aid.', 'Are they your parents?', 'It is not far to New York.', 'It is our wedding anniversary next month.', 'It\'s freezing outside.', 'There\'s no smoke without fire.', 'There is nothing to be seen.', 'There is nowhere to go.', 'The whole thing is a fraud.', 'He is 6 years old.', 'She wore a red dress but a blue suits her better.', 'The hungry and the poor', '(Hot soup!) That\'s what I would like just now!', 'Crystal breaks easily.', 'Crystal breaks at the slightest touch.', 'John is difficult to please.', 'This flat must have cost a lot to furnish.', 'John broke a window. / It was John that broke the window.', 'It was a window John broke.', 'What John needs is a long rest.', 'The thing John needs is a long rest.', '(Do you know him?) I don\'t.', 'Will you go there? I will.', '(That Ford slowed down as we passed.) I wonder why it did that/it.', 'She might sing but I don\'t think she will do so.', '(John buys his drinks at the local supermarket.) So he does.', '(Peter likes tea.) So does Paul.'];
   
    var asFilter = ['a', 'w', 'a w', 'why', 'he is', 'broke'];
   
   
    function testFn(fn) {
      var res = [];
      for (var i=0; i<asFilter.length; i++) {
        res.push(fn(asSentence, asFilter[i]));
      }
      return res;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Raw string operations filtering
function filterRaw(asData, sFilter) {
  var asTerm = sFilter.toLowerCase().split(/\s+/g);
  var oFilter = {aIndex:[], asData:[]};
  for (var i=0, ii=asData.length; i<ii; i++) {
    var bMatch = true;
    for (var j=0, jj=asTerm.length; j<jj && bMatch; j++) {
      bMatch = bMatch && asData[i].indexOf(asTerm[j]) !== -1;
    }
    if (bMatch) {
      oFilter.aIndex.push(i);
      oFilter.asData.push(asData[i]);
    }
  }
  return oFilter;
}
var a = testFn(filterRaw);
pending…
RegExp filtering
function filterRE(asData, sFilter) {
  var arFilter = [];
  var asTerm = sFilter.split(/\s+/g);
  for (var i=0; i<asTerm.length; i++) {
    arFilter.push(new RegExp(asTerm[i].replace(/[^\w\s]/g, "\\$&"), 'i'));
  }
  var oFilter = {aIndex:[], asData:[]};
  for (var i=0, ii=asData.length; i<ii; i++) {
    var bMatch = true;
    for (var j=0, jj=arFilter.length; j<jj && bMatch; j++) {
      bMatch = bMatch && arFilter[j].test(asData[i]);
    }
    if (bMatch) {
      oFilter.aIndex.push(i);
      oFilter.asData.push(asData[i]);
    }
  }
  return oFilter;
}
var b = testFn(filterRE);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment