Prototype vs Revealing

2011-02-20

Below are some benchmark times for my new parser. It runs in O(n) and the benchmark actually tests the Ext.js library, which is 715k minified.

What I specifically tested was using the revealing pattern versus the prototypal pattern. It clearly shows the prototypal pattern is slightly faster on all browsers. What you're seeing is the times from an earlier version of the script, where I was lazy and just wanted it to work (revealing pattern) and the next version, where I refactored everything into the proper prototypal structure.

However, take these results with a grain of salt. The results are very irradic. On top of that, this is when parsing a shitload of text. Not something you'll be doing on your every day scripts. All engines do some kind of optimization and this also affects the results because this optimization is not exactly static. On top of that there can be all kinds of external influences at any given time.

The parser is ran ten consecutive times with a 10ms timer in between. So the optimizer is given a second chance, if it's smart enough to cache prior optimizations for the same script. During my testing (mostly in Chrome11) it was very usual for the first time to be slower than the rest. As you can see, Chrome clearly wins this competition, with Opera closely following. I wanted to test IE8 for kicks but I don't know how to remove the script execution throttle (if that's even possible...). So it's not in here.

The actual parser will be released when I'm done with it :) Although you might see some snapshots on twitter ;)

The general gist of these results, at least for me, is that the prototypal inheritance is faster than the revealing pattern (where you use closures and return an object with "public" methods). But as this benchmark should also clearly show is that it really really really doesn't matter, unless you need seriously high performing code. Oh and on top of that, using prototype also allows you to maintain your code better ;)

I might add ie9 and fx4 results to the page as well, but that's probably not going to be on the same machine
Below the prototypal versions is also the avg speed it currently runs at, mainly after removing "some" function calls, remarkably the difference on firefox is not as significant as I had expected

Only tested on Windows browsers...

# opera 11, revealing pattern
parser time: 827 ms
parser time: 817 ms
parser time: 822 ms
parser time: 820 ms
parser time: 819 ms
parser time: 823 ms
parser time: 819 ms
parser time: 818 ms
parser time: 823 ms
parser time: 832 ms
avg: 822.2 ms

# opera 11, prototypal pattern
parser time: 821 ms
parser time: 782 ms
parser time: 841 ms
parser time: 846 ms
parser time: 768 ms
parser time: 801 ms
parser time: 771 ms
parser time: 827 ms
parser time: 843 ms
parser time: 771 ms
avg: 807.3 ms
(now: avg: 723.8 ms)

# safari 5, revealing pattern
parser time: 1039 ms
parser time: 1090 ms
parser time: 1098 ms
parser time: 1119 ms
parser time: 1117 ms
parser time: 1100 ms
parser time: 1130 ms
parser time: 1120 ms
parser time: 1101 ms
parser time: 1122 ms
avg: 1103.7 ms

# safari 5, prototypal
parser time: 1002 ms
parser time: 990 ms
parser time: 1012 ms
parser time: 971 ms
parser time: 1007 ms
parser time: 1032 ms
parser time: 996 ms
parser time: 1004 ms
parser time: 995 ms
parser time: 1038 ms
avg: 1004.8 ms
(now: avg: 864.4 ms)

# chrome 11, revealing
parser time: 563 ms
parser time: 574 ms
parser time: 581 ms
parser time: 581 ms
parser time: 589 ms
parser time: 618 ms
parser time: 592 ms
parser time: 589 ms
parser time: 612 ms
parser time: 585 ms
avg: 588.5 ms

# chrome 11, prototypal
parser time: 572 ms
parser time: 579 ms
parser time: 587 ms
parser time: 575 ms
parser time: 584 ms
parser time: 580 ms
parser time: 586 ms
parser time: 570 ms
parser time: 580 ms
parser time: 569 ms
avg: 578.5 ms
(now: avg: 477.5 ms)

# firefox 3.6, revealing
parser time: 3170 ms
parser time: 2967 ms
parser time: 2936 ms
parser time: 2973 ms
parser time: 3003 ms
parser time: 3050 ms
parser time: 3090 ms
parser time: 3143 ms
parser time: 3185 ms
parser time: 3334 ms
avg: 3085.1 ms

# firefox 3.6, prototypal
parser time: 3304 ms
parser time: 2988 ms
parser time: 2995 ms
parser time: 3060 ms
parser time: 3035 ms
parser time: 3023 ms
parser time: 3033 ms
parser time: 3050 ms
parser time: 3032 ms
parser time: 3099 ms
avg: 3062 ms
(now: avg: 2963.3 ms)

# IE8, newest version (couldnt bother to wait ~20 minutes for both other tests)
parser time: 12782 ms
parser time: 13813 ms
parser time: 22407 ms
parser time: 53391 ms
parser time: 26734 ms
parser time: 38718 ms
parser time: 30984 ms
parser time: 53609 ms
parser time: 20656 ms
parser time: 64984 ms
avg: 33807.8 ms