Other posts in this series:
Performance may not be a concern on small scripts / widgets that run only once or at most a couple of times per page view, but it should be something to consider when creating a big application with scripts continuously running and a relatively large collection of objects in memory. I decided to run a bunch of of tests on different methods to achieve a same goal covering just cpu performance, because even the most complicated of the applications in javascript running on a 5 year old computer is still very far of being a big memory consumer, unless of course you fall on some type of memory leak.
To build these tests, I did not made any kind of research on the subject. I simply wrote a bunch of scripts and ran them on the two most used browsers, Firefox 2 and Internet Explorer 7, and now I want to share my results and conclusions. Because I ran many different tests I decided to brake the post into a small series of X (still undetermined) posts.
Also, please remember that these results are the results from my computer (actually my wife’s computer) on my particual browser versions. Different computers and different browser versions may lead to different results. If you want to, you may post a comment with your own results, to keep as a reference.
Closures versus object properties
This test add some surprising results for me. I always thought that defining a variable in a closure is more time consuming than defining a object attribute. But… For the purpose of the test I created two constructors:
1 2 3 4 5 6 7 | function propTest() { this.x = 5; } function closeTest() { var x = 5; } |
Easy two see what each one does. Then I created 500000 instances of each constructor inside a for loop, and measured the time (in milliseconds of course) spent on all those iterations. Here are the results:
| Closure | Property | |
| FF2 | 2990 | 4482 |
| IE7 | 4295 | 4982 |
So, as you can see, in neither of the two browsers, setting up properties is faster, although, in IE the values are very close. This was about writing the variables, and what about reading them? Two more constructors for our joy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function closeReadTest() { var x = 5; for (var i = 0; i < 500000; i++) { x; } } function propReadTest() { this.x = 5; for (var i = 0; i < 500000; i++) { this.x; } } |
Each one of these sets up a closure or a property only once, and then reads it 500000 times. The results:
| Closure | Property | |
| FF2 | 54 | 87 |
| IE7 | 99 | 169 |
And closures win again (and FF to). Now for the next test I considered the following: closures are nice, but sometimes we need to access a certain value outside the object, so we have to write getter method… So, let’s do it. I used the propTest constructor of the first test, and this one:
1 2 3 4 5 6 7 | function closeExtTest() { var x = 5; this.getX = function() { return x; } } |
I created a instance of each and read x 500000 times. Now be prepared for what’s coming:
| Closure | Property | |
| FF2 | 1478 | 1861 |
| IE7 | 3110 | 1797 |
That’s it, in FF is still about 25% faster to use closures, even with an extra method, but in IE, we get the double of the time on the usage of closures versus about the same time with the object properties. The final and obvious test is external writing to an object. Once again I used the propTest constructor of the first test, and a new one with a setter to use on the closure test:
1 2 3 4 5 6 7 | function closeExtWriteTest() { var x = 5; this.setX = function(_x) { x = _x; } } |
Then, after the usual 500000 times loop, here are the results:
| Closure | Property | |
| FF2 | 1525 | 1869 |
| IE7 | 3206 | 1831 |
Almost the same results of the external reading test. This leaves me to a conclusion, that is not any different of what I’m used to do. If you want your methods to be public use object properties, if you want to keep them just for your self, don’t be afraid to use closures. You can run the tests yourself here, but please wait about 15 seconds to compute the results before trying to kill the browser process.
Stay tuned for the remaining tests. Hope this helps someone.
Cheers,
Hernâni
This entry was posted on Thursday, December 13th, 2007 at 8:42 pm and is filed under JavaScript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


























































Nice work. Great to see some proof of what I also believed to be true. The strange IE Closure results in the last two tests is surprising, then again not, IE is notorious for nonintuitive behavior.
Any ideas as to why their results are so skewed in those last tests? I suspect it’s the overhead of using a method to manipulate the variable versus direct property manipulation. In the very least it’s multiple operations versus just 1, but the skew is still surprising…
FYI, Safari 3.0.3 on XP gives the following results, Opera 9 pretty much does the same:
Closures versus object attributes
Writing
Closures - 1516
Properties - 1516
Reading
Closures - 219
Properties - 218
External Reading
Closures - 1407
Properties - 797
External Writting
Closures - 1547
Properties - 860
Looks like properties are faster in Opera and Safari…at least on XP.
Have you tested in IE and FF? Today I made a try on my work computer which is a linux box, and I got similar results in FF. I also have IE installed trough wine, but it’s so unstable that all the tests crashed the browser.
On the MacBook Pro, I tested FF2 and Safari 3, and found the closures are faster, except when used for external reading/writing. Here are the numbers:
FF2:
Closures versus object attributes
Writing
Closures - 2714
Properties - 3827
Reading
Closures - 24
Properties - 45
External Reading
Closures - 1827
Properties - 1587
External Writting
Closures - 1894
Properties - 1591
Safari 3:
Closures versus object attributes
Writing
Closures - 761
Properties - 702
Reading
Closures - 116
Properties - 117
External Reading
Closures - 587
Properties - 371
External Writting
Closures - 642
Properties - 395
Buy viagra online….
Buy viagra online. Where to buy viagra. Buy viagra. Buy viagra uk. Viagra uk buy viagra online order viagra….