Peculiar Cross-Browser Regular Expression performance

I just tried a quick test to see which of two regular expression techniques would perform better. I was quite surprised to find that different browsers gave me different results for different techniques. Now, this is in no way a truly scientific result and I'd be interested to see how it plays out on multiple systems and on multiple browsers.

I put together the following code to test my two methods of testing a regular expression:

    var a = new Date();
    for(var i=0;i<10000;i++){
        /./.test("string");
    }
    var b = new Date();

    var c = new Date();
    for(var i=0;i<10000;i++){
        "string".search(/./);
    }
    var d = new Date();

    alert((b.getTime()-a.getTime()) +"|"+ (d.getTime()-c.getTime()));

If you click on "Test Function" you should be spit back the results, which happens to be time in milliseconds it takes to complete the task. Here are my results:

Average Regular Expression Results in Milliseconds
Browser Using Test method Using Search method
Firefox 1.0 72.2 176.2
Opera 7.50 137.5 375.5
Internet Explorer 6.0 150.2 122.6

I fully expected that using the Test method would work faster since it simply returns true or false. The Search method is a little more complicated in that it returns an integer of the position in which the first match is found.

Firefox and Opera seem to support my hypothesis, while Internet Explorer doesn't.

What does this all mean? In the end, likely very little. Most of us use JavaScript for small utility tasks and tweaking performance would produce no noticable effect. However, if you do have numerous tests to perform (like iterating through a 10,000 node DOM) then this information may be of use.

Run the script and let me know if you run into different results. Be sure to indicate what browser and OS you're running on.

Published January 27, 2005 · Updated September 17, 2005
Categorized as JavaScript
Short URL: https://snook.ca/s/321

Conversation

8 Comments · RSS feed
Julian said on January 27, 2005

I get different results every time I click on it, mostly the first number is smaller.
63|94 and quite often 31|31
Firefox and Windows XP

Jehiah said on January 27, 2005

Safari 1.2.4 : 319/357
IE 5.2 Mac : 195/124
--
IE 6.0 Win : 47/31
Firefox 1.0 Win : 16/62

It would apear that IE shows the same results in all versions on all platforms. Interesting indeed.

Matt Wilson said on January 27, 2005

Mac OS X 10.3.7, G4 667MHz

Firefox 1.0: 101/192
Safari 1.2.4: 787/869

Safari seems to be massively slower - although it may be due to the number of tabs I have open.

Stefan said on January 27, 2005

32/63 >> FF1.0 on winXP.

94/78 >> IE5.5 standalone on winXP.
73-110/73-110 >> IE6 on winXP.

Wow. IE6 is giving some pretty spaced out results.

Calrion said on January 27, 2005

Quite often 50|90 but have seen as low as 40|85. Went as high as 65|105.

FF1.0 on WinXPSP2 Pro. Athlon 1200 with 1024MB PC133 SDRAM.

IE6.0SP2 on same machine: 80|70 most of the time, once 70|70. Otherwise search was consistently lower than test.

Waylan said on January 28, 2005

Wow, guess my machine is really slow. But then again, it is an older Win 98 box. What seems really strange to me is how the time increased nearly every time I ran the test. I ran it four times in Firefox 1.0 and got these four results:
990|1210
1210|1430
1320|1430
1210|1650
Strange huh?

Mat?as said on January 28, 2005

My results are also based on my machine: PII 266 overclocked to 500Mhz, 128Mb Ram, Win98SE and Firefox 1.0 (with Thunderbird and Winamp running too).

Tried it a couple of times and these was what I got:

2420|3630
2200|2690
2250|3080
2250|2640
2200|2740

I tried using IE 6 (kept FF, TB and Winamp running) and got:

770|330
490|330
550|330
440|330
500|330

My guess is that IE has a hack, modification or some kind of optimization for the search. I base this not only on getting always the same result on search but also on the important decrease of the results.

Simon Zimmermann said on January 28, 2005

Tested on my old computer(PII, 300mhz, 500RAM) using FF1.0.

1, 270|641
2, 280|681
3, 280|651

Testing on IE6

1, 541|401
2, 471|401
3, 500|339

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.