<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Kristoffer's tidbits : Javascript performance</title><link>http://blogs.msdn.com/kristoffer/archive/tags/Javascript+performance/default.aspx</link><description>Tags: Javascript performance</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Javascript prototype versus closure execution speed</title><link>http://blogs.msdn.com/kristoffer/archive/2007/02/13/javascript-prototype-versus-closure-execution-speed.aspx</link><pubDate>Tue, 13 Feb 2007 19:49:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1670057</guid><dc:creator>Kristoffer</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/kristoffer/comments/1670057.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kristoffer/commentrss.aspx?PostID=1670057</wfw:commentRss><description>&lt;p&gt;When Javascript&amp;nbsp;execution speed matters consider using prototype instead of closures where possible. Even for a small closure the difference can be noticable when called hundreds of times. Consider the following example that implements a Pixel object with some ancillary functions in both the closure and prototype patterns:&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Closure implementation&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;function&lt;/span&gt; Pixel(x, y)&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.x = x;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.y = y;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.getX = &lt;span style="color: blue"&gt;function&lt;/span&gt;()&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.x;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.getY = &lt;span style="color: blue"&gt;function&lt;/span&gt;()&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.y;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.setX = &lt;span style="color: blue"&gt;function&lt;/span&gt;(value)&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.x = value;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.setY = &lt;span style="color: blue"&gt;function&lt;/span&gt;(value)&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.y = value;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;// Prototype implementation&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;function&lt;/span&gt; PixelP(x, y)&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.x = x; &lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.y = y;&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;p style="margin: 0px"&gt;PixelP.prototype.getX = &lt;span style="color: blue"&gt;function&lt;/span&gt;()&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.x;&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;p style="margin: 0px"&gt;PixelP.prototype.getY = &lt;span style="color: blue"&gt;function&lt;/span&gt;()&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.y;&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;p style="margin: 0px"&gt;PixelP.prototype.setX = &lt;span style="color: blue"&gt;function&lt;/span&gt;(value)&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.x = value;&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;p style="margin: 0px"&gt;PixelP.prototype.setY = &lt;span style="color: blue"&gt;function&lt;/span&gt;(value)&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.y = value;&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;function&lt;/span&gt; TestPerformance()&lt;/p&gt; &lt;p style="margin: 0px"&gt;{&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; closureStartDateTime = &lt;span style="color: blue"&gt;new&lt;/span&gt; Date();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; i = 0; i &amp;lt; 20000; i++)&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; x = &lt;span style="color: blue"&gt;new&lt;/span&gt; Pixel(i, i);&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; closureEndDateTime = &lt;span style="color: blue"&gt;new&lt;/span&gt; Date();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; prototypeStartDateTime = &lt;span style="color: blue"&gt;new&lt;/span&gt; Date();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; i = 0; i &amp;lt; 20000; i++)&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; x = &lt;span style="color: blue"&gt;new&lt;/span&gt; PixelP(i, i);&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; }&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; prototypeEndDateTime = &lt;span style="color: blue"&gt;new&lt;/span&gt; Date();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; closureTime = closureEndDateTime.getTime() - closureStartDateTime.getTime();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; prototypeTime = prototypeEndDateTime.getTime() - prototypeStartDateTime.getTime();&lt;/p&gt; &lt;p style="margin: 0px"&gt;&amp;nbsp; alert(&lt;span style="color: #a31515"&gt;"Closure time: "&lt;/span&gt; + closureTime + &lt;span style="color: #a31515"&gt;", prototype time: "&lt;/span&gt; + prototypeTime);&lt;/p&gt; &lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/div&gt;&lt;!--EndFragment--&gt;&lt;/div&gt;&lt;!--EndFragment--&gt;&lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;When this is run in IE7 on my machine, the closure implementation takes around 450ms and prototype takes around 140ms. Add two more empty functions and the closure time increases to 560ms and prototype to 155ms.&lt;/p&gt; &lt;p&gt;On Firefox 1.5.0.9 the difference is in the listed example is even larger with the closure taking 515ms and prototype still around 140ms. &lt;/p&gt; &lt;p&gt;For small applications that don't construct hundreds or thousands of the same type of object the execution speed difference between closure and prototype probably doesn't matter so as with all performance advice measure, measure, measure.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1670057" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kristoffer/archive/tags/Javascript+performance/default.aspx">Javascript performance</category></item></channel></rss>