<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lea Verou &#187; Benchmarks</title>
	<atom:link href="http://lea.verou.me/category/benchmarks/feed/" rel="self" type="application/rss+xml" />
	<link>http://lea.verou.me</link>
	<description>Life at the bleeding edge (of web standards)</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:08:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CSS gradients are faster than SVG backgrounds</title>
		<link>http://lea.verou.me/2011/08/css-gradients-are-much-faster-than-svg/</link>
		<comments>http://lea.verou.me/2011/08/css-gradients-are-much-faster-than-svg/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 01:05:51 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Benchmarks]]></category>
		<category><![CDATA[CSS gradients]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[SVG]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=1188</guid>
		<description><![CDATA[Which is really sad, because SVG is awesome. It lets you do what CSS gradients do and much more, in quite a small filesize, as it’s just text too. However, the browser needs to generate a DOM for every SVG graphic, which results in sluggishness. Here&#8217;s my test case Mouse over the 2 divs. They both [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2011%252F08%252Fcss-gradients-are-much-faster-than-svg%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FpXXUrC%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22CSS%20gradients%20are%20faster%20than%20SVG%20backgrounds%22%20%7D);"></div>
<p>Which is really sad, because SVG is awesome. It lets you do what CSS gradients do and much more, in quite a small filesize, as it’s just text too. However, the browser needs to generate a DOM for every SVG graphic, which results in sluggishness.</p>
<p><a class="view-demo" href="http://jsfiddle.net/leaverou/8hQEy/embedded/result%2Ccss%2Cjs/" target="_blank">Here&#8217;s my test case</a></p>
<p>Mouse over the 2 divs. They both use a spotlight effect that&#8217;s dynamically updated according to the position of the mouse cursor. One of them does it with an SVG (through a data URI), the other one through a CSS radial gradient.</p>
<p>The test only works in <strong>Chrome</strong>, <strong>Firefox nightlies</strong> and <strong>perhaps IE10</strong> (haven&#8217;t tested in Windows). Why? Because <strong>Opera</strong> doesn&#8217;t support radial gradients yet (however you can see how slow SVG is in it too), and <strong>Firefox</strong> before the nightlies used to have <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=308590" target="_blank">a bug with gradients in SVG through data URIs</a>. Also, jsFiddle seems not to work in <strong>Webkit nightlies</strong> for some reason, but I&#8217;m too lazy right now to make a self-hosted test case.</p>
<p>Thanks a lot to <a href="http://twitter.com/#!/__chris__" target="_blank">Christian Krebs</a> (lead developer of Opera Dragonfly) who inspired these tests after a discussion we had today regarding CSS gradient performance.</p>
<p>Edit: According to some commenters, they&#8217;re the same speed on Windows and Linux, so it could be an OSX issue. The only way to know for sure is to post more results, so go ahead and post yours!</p>
<p>Also, some commenters say that this is not a fair comparison, because it generates a new SVG every time. I have several arguments to reply to this:</p>
<ol>
<li>We also generate a new gradient every time, so it is fair.</li>
<li>You can&#8217;t manipulate an SVG used for a background, so it&#8217;s not an option for backgrounds. JS doesn&#8217;t run in it and you don&#8217;t have access to its DOM. The only way to do that would be to use an inline SVG embedded in HTML and the element() CSS3 function. However, that&#8217;s only supported by Firefox, so not really a pragmatic option.</li>
</ol>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2011/08/css-gradients-are-much-faster-than-svg/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>&#8220;Appearances can be deceiving Mr. Anderson&#8221; &#8211; a.k.a. short code is not always fast code</title>
		<link>http://lea.verou.me/2009/02/appearances-can-be-deceiving-mr-anderson-aka-short-code-is-not-always-fast-code/</link>
		<comments>http://lea.verou.me/2009/02/appearances-can-be-deceiving-mr-anderson-aka-short-code-is-not-always-fast-code/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 01:12:11 +0000</pubDate>
		<dc:creator>Lea Verou</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Benchmarks]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[type checks]]></category>

		<guid isPermaLink="false">http://leaverou.me/?p=182</guid>
		<description><![CDATA[I used to take pride in my short, bulletproof and elegant String and Number type checks: // Check whether obj is a Number obj + 0 === obj // Check whether obj is a String obj + '' === obj I always thought that apart from being short and elegant, they should be faster. However, [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_hot-pink" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Flea.verou.me%252F2009%252F02%252Fappearances-can-be-deceiving-mr-anderson-aka-short-code-is-not-always-fast-code%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22%5C%22Appearances%20can%20be%20deceiving%20Mr.%20Anderson%5C%22%20-%20a.k.a.%20short%20code%20is%20not%20always%20fast%20code%22%20%7D);"></div>
<p>I used to take pride in my short, bulletproof and elegant String and Number type checks:</p>
<pre>// Check whether obj is a Number
obj + 0 === obj

// Check whether obj is a String
obj + '' === obj</pre>
<p>I always thought that apart from being short and elegant, they <strong>should </strong>be faster.</p>
<p>However, some quick tests gave me a cold slap in the face and proved my assertion to be entirely false. <span id="more-182"></span>When comparing the following 4 methods for string and number type checking:</p>
<ol>
<li>&#8220;My&#8221; method (mentioned above)</li>
<li>Object.prototype.toString method: <code>Object.prototype.toString.call(obj) === '[object String]'</code> or  <code>Object.prototype.toString.call(obj) === '[object Number]'</code></li>
<li>Typeof method: <code>typeof obj === 'string'</code> or <code>typeof obj === 'number'</code></li>
<li>Contructor method: <code>obj.constructor === String</code> or <code>obj.constructor === Number</code></li>
</ol>
<p>It turned out that the Object.prototype.toString method was <strong>50%</strong> faster than my method, and both typeof and constructor methods were a whopping <strong>150%</strong> faster than my method! No wonder <a href="http://docs.jquery.com/JQuery_Core_Style_Guidelines" target="_blank">jQuery uses the typeof method for their String/Number tests</a>.</p>
<p>Now that I think about it, it does actually make sense &#8211; my method converts <code>obj</code> to a String or Number, then concatenates/adds it with another String/Number, then compares value and type. Too much stuff done there to be fast. But I guess I was too innocent and subconsciously thought that it wouldn&#8217;t be fair if elegant and short code wasn&#8217;t fast too.</p>
<p>Of course the overall time needed for any of these tests was neglible, but it&#8217;s a good example of how much appearances can be deceiving &#8211; even in programming! <img src='http://lea.verou.me/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The moral: Never assume. Always test.</p>
<h3>So, which method is ideal for String/Number checks? (added afterwards)</h3>
<p>The typeof method and my method fail for non-primitive String/Number objects, as you can easily observe if you type in the console:</p>
<pre>typeof new String('foo') // 'object'
typeof new Number(5) // 'object'
new String('foo') + '' === new String('foo') // false</pre>
<p>This can easily be solved if you also check the type via instanceof (the decrease in speed is negligible):</p>
<pre>foo = new String('foo');
typeof foo === 'string' || foo instanceof String
foo + '' === foo || foo instanceof String</pre>
<p>Don&#8217;t use instanceof alone, since it fails for String and Number primitives. The instanceof method also fails for Strings and Numbers created in another window, since their constructor there is different. Same happens with the Constructor method mentioned above.</p>
<p>It seems that if you need a <strong>bulletproof </strong>check the only method you can use is the Object.prototype.toString method and luckily, it&#8217;s one of the fastest (not the fastest one though), so I guess we can safely elect it as the ideal method for String and Number checks (and not only for arrays, as it was first made popular for).</p>
<p><span style="font-size:smaller">PS: For anyone wondering what the quote in the title reminds him/her, its from the <em>Matrix Revolutions</em> movie.</span></p>

]]></content:encoded>
			<wfw:commentRss>http://lea.verou.me/2009/02/appearances-can-be-deceiving-mr-anderson-aka-short-code-is-not-always-fast-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

