<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>BlackBerry Developer Blog &#187; DOM</title>
	<atom:link href="http://devblog.blackberry.com/tag/dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://devblog.blackberry.com</link>
	<description></description>
	<lastBuildDate>Thu, 23 May 2013 19:58:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='devblog.blackberry.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/9ef0a66c09615fa946c4179662398878?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>BlackBerry Developer Blog &#187; DOM</title>
		<link>http://devblog.blackberry.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://devblog.blackberry.com/osd.xml" title="BlackBerry Developer Blog" />
	<atom:link rel='hub' href='http://devblog.blackberry.com/?pushpress=hub'/>
		<item>
		<title>Using Web Inspector to get a 3000% performance boost</title>
		<link>http://devblog.blackberry.com/2012/07/web-inspector-performance-boost/</link>
		<comments>http://devblog.blackberry.com/2012/07/web-inspector-performance-boost/#comments</comments>
		<pubDate>Tue, 03 Jul 2012 15:35:11 +0000</pubDate>
		<dc:creator>Adam S.</dc:creator>
				<category><![CDATA[Case Studies & Success Stories]]></category>
		<category><![CDATA[Java Development]]></category>
		<category><![CDATA[blackberry playbook]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[Reader Mode]]></category>
		<category><![CDATA[Web Inspector]]></category>

		<guid isPermaLink="false">http://devblog.blackberry.com/?p=10035</guid>
		<description><![CDATA[A case study for using BlackBerry Web Inspector to help speed up developmental performance.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devblog.blackberry.com&#038;blog=17235680&#038;post=10035&#038;subd=rimdevblog&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><em>Here&#8217;s a guest post from Konrad to convince you of the powers of Web Inspector! &#8211; Adam S.</em></p>
<p><img src="http://rimdevblog.files.wordpress.com/2012/07/web-inspector-8.jpg?w=550&#038;h=322" alt="TITLE_IMAGE" title="TITLE_IMAGE" width="550" height="322" class="aligncenter size-full wp-image-10056" /></p>
<p>Have you ever used that cool button on your <a href="http://www.blackberry.com/playbook" target="_new">BlackBerry® PlayBook™</a> tablet Browser that looks like a book? It’s the icon in the top left that enables Reader Mode. This is a neat feature which takes away all the advertisements and extraneous features from the page and shows you a clean summary of the content. Today I wanted to tell you about how this feature came to be, how we optimized its performance, and how it would not have been possible without BlackBerry® Web Inspector.</p>
<p>Using some clever JavaScript®, we created a mechanism that extracted the page contents and stripped out unwanted HTML. The problem was that it took a really long time to parse a page and display the content &#8212; or so we thought. We knew that this would not be a good user experience and the page load time needed to be optimized, especially when compared to the better performance we observed for the same content running in a desktop browser.</p>
<p><span id="more-10035"></span></p>
<p>As I started my investigation, I wanted to look at the JavaScript to see how it worked so I could start to tweak it, and I thought to myself, &#8220;I should use Web Inspector to profile this.&#8221; I didn&#8217;t know it yet but this idea would save me lots of time and effort to get the feature working.</p>
<p>After adding some profiling information to the script, I discovered that most of the code was already relatively fast. However, after it had reloaded the page in a new WebView, it was doing its removal of the unwanted tags on the live DOM. This incurred a whole page request with sub-resources and content that we were going to be stripped out as soon as the page loaded.</p>
<p>Instead of incurring a page load, I did a deep copy of the DOM using <code>document.body.cloneNode(true)</code>, and then manipulated the HTML fragment off DOM. Finally I loaded the resulting HTML in a new WebView. The result of this change was an immediate gain in performance, reducing the amount of processing time from 45 seconds down to less than 5 seconds. But that wasn&#8217;t enough &#8212; no user should have to wait 5 seconds after hitting a button to see any results.</p>
<p>I then enlisted the help of a colleague, and we were able to optimize the script even further by making changes to our logging. If you log things in JavaScript inline via <code>console.log()</code>, they&#8217;re expensive. Instead, add your log message to a buffer and then print the results after the main processing is complete. The DOM was traversed several times during the algorithm&#8217;s run. We ended up using a TreeWalker instead of a for loop of <code>document.getElementsByTagName(“*”)</code> being used. With these simple changes and further optimizations to the script, we further reduced the page processing time from 5 seconds down to under 1.5, which in and of itself is still a gain of over 300%.</p>
<p>So in summary, we went from 45 seconds down to less than 1.5 seconds &#8212; an astonishing 3000% performance boost that we could not have done without Web Inspector running on device.</p>
<p><strong>Lessons:</strong></p>
<ol>
<li>Add logs to your scripts for performance profiling, but buffer them and don&#8217;t print to console right away.</li>
<li>Don&#8217;t reload the page when you don&#8217;t need to. If you want to modify the DOM, use HTML fragments.</li>
<li>Profile individual methods and try different things. You&#8217;d be surprised how changing one line can make all the difference.</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rimdevblog.wordpress.com/10035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rimdevblog.wordpress.com/10035/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devblog.blackberry.com&#038;blog=17235680&#038;post=10035&#038;subd=rimdevblog&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devblog.blackberry.com/2012/07/web-inspector-performance-boost/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/16f032fc46b780f6d9fa38b24a7f8ff2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">adamstan1</media:title>
		</media:content>

		<media:content url="http://rimdevblog.files.wordpress.com/2012/07/web-inspector-8.jpg" medium="image">
			<media:title type="html">TITLE_IMAGE</media:title>
		</media:content>
	</item>
	</channel>
</rss>
