<?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>Brian Egan &#187; JavaScript</title> <atom:link href="http://brianegan.com/category/javascript/feed/" rel="self" type="application/rss+xml" /><link>http://brianegan.com</link> <description>Designer and Developer</description> <lastBuildDate>Thu, 07 Oct 2010 07:10:31 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.3</generator> <item><title>Fight Recap: NodeKO</title><link>http://brianegan.com/fight-recap-nodeko/</link> <comments>http://brianegan.com/fight-recap-nodeko/#comments</comments> <pubDate>Fri, 03 Sep 2010 07:28:21 +0000</pubDate> <dc:creator>Brian</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[NodeJS]]></category><guid
isPermaLink="false">http://brianegan.com/?p=106</guid> <description><![CDATA[This past weekend I had the honor of participating in the fearsome Rocky-IV-style hackathon that was Node Knockout. For those who may actually have a life: Node Knockout was a 48 hour competition featuring teams of hackers each pecking away at a web application that should be somewhat complete-ish after at the end of the two days. In [...]]]></description> <content:encoded><![CDATA[<p>This past weekend I had the honor of participating in the fearsome Rocky-IV-style hackathon that was <a
href="http://nodeknockout.com/">Node Knockout</a>. For those who may actually have a life: Node Knockout was a 48 hour competition featuring teams of hackers each pecking away at a web application that should be somewhat complete-ish after at the end of the two days. In our case, we tried to build a fairly full-feature music player in 2 days (<a
href="http://vegasjs.no.de/">yeah lets see how that turned out</a>)&#8230;</p><p><span
id="more-106"></span></p><p>Ok, so if you followed the previous link and checked out our app, you&#8217;ll see that it&#8217;s somewhat pretty (for a ripoff), and barely functional! Man we rock&#8230; but really, the competition wasn&#8217;t just about winning for our team as much as it was about fiddling around with some new JavaScript hawtsawce. So here&#8217;s what I learned:</p><h2>Pick your 48 hour project Wisely</h2><p>Yeah, so our <em>idea </em>was to actually build a NodeJS backend which could scan the directories input by the user, read the id3 tag info from the files within those directories (as a start, we could support ogg and flac and other format metadata schemes later), and then put the metadata into a CouchDB.</p><p>Then the client could connect to your server, grab the catalog from NodeJS, and play songs that NodeJS would convert on the fly into the proper format for your browser depending on capability (probably through a child process exec to some other encoder &#8212; haven&#8217;t figured that part out yet).</p><p>Unfortunately, we hit a snag on step 1 of this process: reading the data from an id3 tag, which is binary data stored at the beginning on an mp3 file. Node can read streams of data, like so:</p><pre name="code" class="javascript">  var s = fs.createReadStream(file, {start: 1, end: 2});

  s.on('data', function(data){
    console.log(data.toString());
  });</pre><p>This is actually the code I was using to check whether a particular file begins with &#8220;ID3,&#8221; which would thus signify ID3 version 2 data. All the fun technical details behind ID3 can be read about here: <a
href="http://www.id3.org/Developer_Information">http://www.id3.org/Developer_Information</a></p><p>But this is about when things started to break down. After this, and I&#8217;m literally talking about the first 10 bytes here, became totally beyond my grasp for a 48 hour competition. WTF does this mean, for example:</p><blockquote><p> The ID3v2 tag size is stored as a 32 bit synchsafe integer (section<br
/> 6.2), making a total of 28 effective bits (representing up to 256MB).</p></blockquote><p>I mean, I somewhat get what that&#8217;s saying. If you just read section 6.2, you can clear as day see&#8230;</p><p>All this led me to one conclusion after a few hours: <strong>Choose your project wisely</strong>. Dylan Bathurst, who I&#8217;m officially dubbing my &#8220;Node Wingman,&#8221; is a Front end guy. I&#8217;m a front end guy. We probably should not have picked a project that not only required us to pick up server-side programming, but also get all Computer Science major trying to decipher binary data (I have a degree in Sociology &#8212; super helpful for that kind of thing). It seems like a simple game using WebSockets might have been a better call&#8230; but oh well, just pick a project that helps you learn Node and plays to your strengths!</p><h2>Plan a bit in advance&#8230;</h2><p>We did a bit of preliminary planning, but we really didn&#8217;t get into the nitty gritty of our roles and responsibilities until the competition began. Not that this isn&#8217;t a totally fun way to approach a 48 hour competition, and if you think Animal from the Muppets is the coolest guy ever, then just jumping headfirst into the competition might be the best thing for you.</p><p>But, it seemed like some of the <a
href="http://www.startupmonkeys.com/2010/09/building-a-scrabble-mmo-in-48-hours/">best ideas</a> had been well thought out a in advance, including splitting up roles for each team member so they could focus on their tasks. With only 2 days of actual design and programming time, getting the prep out of the way so you can focus on code is clutch.</p><h2>The community rocks</h2><p>Yeah, I&#8217;m talking to you. I hadn&#8217;t spent much time in IRC before that weekend, but I was able to ask questions and get prompt, helpful responses. Plugin creators took their time to explain basic stuff to me, and it was even easy to hit up the creator of NodeJS. Plus, the vibe was just electric. Man, it was just fun being in there with everyone.</p><p>I also gotta say that I was really impressed with the <a
href="http://joyent.com">Joyent</a> setup overall. Thanks for providing such an awesome platform on which to host the competition!</p><h2>Even if you don&#8217;t know anything about NodeJS, you should still totally participate</h2><p>I wasn&#8217;t completely ignorant of Node, but I&#8217;m by no means an expert, and I haven&#8217;t built any serious/real apps on it. Heck, I hadn&#8217;t even built much of a toy app with it. This was my first real attempt at anything of substance. And even though we didn&#8217;t get a fully-functional app going, I learned more in two days than I have in a long time.</p><p>I would encourage anyone who has even the smallest inclination to give Node a shot to join this competition next time it comes around. If you haven&#8217;t learned Node because you don&#8217;t really know what to do with it, this competition ends that excuse! Just dive in. The water is warm &#8212; I promise!</p><h2>The future is lookin pretty good&#8230;</h2><p>And you know what was so cool about this competition? Knowledge wasn&#8217;t the best thing I gained. I walked away with a sense of optimism I haven&#8217;t felt in a while. When you get into the daily grind, a lot of the knowledge we accumulate seems to be incremental rather than revolutionary. NodeJS is a revolution.</p><p>Now, of course, there are a couple of secrets I&#8217;ve gotta save for myself until next year to give myself a bit of a competitive edge (heyo!), but I gotta end this post thanking everyone who put the competition together, Joyent again for hosting, the awesome people who were hangin out in IRC, and my buddy Dylan for putting the app together with me! It was a blast, and you&#8217;d better believe we&#8217;re gonna win it next year, so be ready to bring it!</p> ]]></content:encoded> <wfw:commentRss>http://brianegan.com/fight-recap-nodeko/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Book Review: Advanced JavaScript by TJ Holowaychuck</title><link>http://brianegan.com/book-review-advanced-javascript-by-vision-media/</link> <comments>http://brianegan.com/book-review-advanced-javascript-by-vision-media/#comments</comments> <pubDate>Mon, 29 Mar 2010 02:41:57 +0000</pubDate> <dc:creator>Brian</dc:creator> <category><![CDATA[JavaScript]]></category><guid
isPermaLink="false">http://brianegan.com/?p=44</guid> <description><![CDATA[I&#8217;m not sure if it&#8217;s been done before, but I may very well be the first GitHub stalker (hold your applause, please). Who is my target you ask? Vision Media, aka TJ Holowaychuk on GitHub, author of ExpressJS, a Sinatra-inspired Web Framework written for NodeJS. After cloning and examining a lot of his repos (Use [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;m not sure if it&#8217;s been done before, but I may very well be the first GitHub stalker (hold your applause, please). Who is my target you ask? <a
href="http://github.com/visionmedia">Vision Media</a>, aka TJ Holowaychuk on GitHub, author of <a
href="http://expressjs.com/">ExpressJS</a>, a Sinatra-inspired Web Framework written for NodeJS.</p><p>After cloning and examining a lot of his repos (Use the Source!), I found out in an endnote that TJ had published a book entitled <a
href="http://www.dev-mag.com/2010/02/18/advanced-javascript/">Advanced JavaScript</a> from <a
href="http://www.dev-mag.com/">DevMag</a>. It&#8217;s a short book, but one that opened my eyes in a surprising number of ways.<br
/> <span
id="more-44"></span></p><h2>Overview</h2><p>The books Table of Contents are as follows:</p><ul><li>Ch 1: Closures, Scope and Context</li><li>Ch 2: Prototypal Inheritance</li><li>Ch 3: Advanced Meta-programming techniques</li><li>Ch 4: Behavior Driven Development</li><li>Ch 5: Creating a jQuery Clone</li><li>Ch 6: Tools of the trade</li></ul><p>The meat of this review will focus on Chapters 3-5, which made me rethink the way I develop JavaScript. No offense to the author(s), but the contents in Chapters 1 and 2 have been better explained in other texts, notably <a
href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1269827039&amp;sr=8-1">JavaScript: The Good Parts</a> by Douglas Crockford, and Chapter 6 should have probably just been an Appendix.</p><p>But back to the good stuff: Chapter 3 goes through the creation of a JavaScript clone of some of the most basic functionality found in Sinatra, namely creating routes and executing them when matched. While the concept of DRY is often promoted as The Thing To Do, this chapter clearly explained some of the ways to make your programs smarter so that you can in fact avoid repetition! Very nice.</p><h2>The revolution will not be televised, it will be tracked on GitHub</h2><p>The revolution started for me in Chapter 4. I&#8217;d read about some agile programming techniques, but I&#8217;d never heard of BDD, or Behavior Driven Development. Chapter 4 talks about how to implement BDD with JavaScript using JSpec, an awesome unit testing framework, and one that totally changed how I think about my projects.</p><h3>An aside on BDD</h3><p>Essentially, BDD is an &#8220;outside-in&#8221; methodology, meaning that you start by identifying outcomes with your clients (internal or external), and then map out the features that will achieve such outcomes. This often happens in a story format.</p><p>Overall, it&#8217;s fantastic because it allows your clients to stay on the same page as you, the developer. You define concrete outcomes with your clients, and which steps needed to be taken for each feature to work. This means that both you and your clients have a solid understand of what needs to be done, and what &#8220;done&#8221; truly means. To read more about BDD specifically, check out <a
href="http://blog.dannorth.net/whats-in-a-story/">a great explanation of BDD</a> from the inventor, Dan North.</p><h3>Using JSpec to implement BDD</h3><p>With regard to chapter 4, JSpec helps us implement BDD from the developers perspective. Once the outcomes and features have been hashed out into specific steps, or &#8220;units,&#8221; then the developer can begin doing his/her job! Now, BDD is an extension of test driven development, and therefore implies that we should begin not by writing our implementation, but by writing Unit Tests to ensure that the functionality defined within our story is properly executed! By working this way, developers get the myriad of benefits that come from proper unit testing, such as avoiding regressions (breaking your code).</p><p>However, it had never really clicked in my mind how to really do convenient / good unit testing with JavaScript, and that&#8217;s what Chapters 4 and 5 go into in detail. There are a ton of libraries out there, but none of them encourage Unit Testing as part of the workflow the way JSpec does, because most of them are just simple unit testing frameworks written in JavaScript. But JSpec is so much more!</p><p>Jspec doesn&#8217;t just include a unit testing JavaScript file, you download a whole Ruby gem and get amazing command-line utilities for creating new projects with testing and documentation all set up, and then very convenient methods for running the unit tests in all your browsers! For example, if you execute the following command:</p><p><code>jspec init mySpecTest</code></p><p>You&#8217;ll get a new folder called mySpecTest with a directory full of goodies, such as a convenient folder structure for creating consistent projects, and all you need to run the JSpec unit tests. With the specs from your client in hand, you could then quickly begin writing your unit tests to achieve the desired functionality! Then all you have to do is write your JavaScript to pass all of your tests, and you have a very discrete piece of functionality &#8220;done!&#8221;</p><h2>Wrap Up</h2><p>The book does a great job of going through how to use JSpec in a number of ways, and how to build a cool, limited jQuery clone using the principles of BDD. Overall, as I said, this has literally changed the way I think about writing JavaScript in the future. This will certainly be the approach I take (I had never really written rigorous tests for my JS before), but one that I can immediately see will provide a ton of benefits to the quality of my code.</p><p>If you&#8217;re like me, and you thought &#8220;Unit testing is pretty cool, but I really have no idea how to properly implement it,&#8221; you will walk away from this book knowing exactly how to use unit tests, and you&#8217;ll also probably be bursting with ideas about how you can improve your code and your development methodologies.</p><p>&#8220;Advanced JavaScript&#8221; rules, and for $4 it is well worth the price. <a
href="http://www.dev-mag.com/2010/02/18/advanced-javascript/">Go buy it</a>.</p><h2>Addendum/Nitpicks</h2><p>Now, while the book totally kicks ass, I did have some minor problems with it. I really think the book needs a good editor. There are some frankly just kind of sloppy grammatical and spelling errors throughout the book, and the writing in general could use a good solid critique to make it a bit better. Also, there&#8217;s a lot of source code presented in the book, but the book didn&#8217;t come with all of the source in separate files. It had a few files, but it was missing some of the JSpec files that I would have found most interesting.</p> ]]></content:encoded> <wfw:commentRss>http://brianegan.com/book-review-advanced-javascript-by-vision-media/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>I heart Node.js</title><link>http://brianegan.com/i-heart-node-js/</link> <comments>http://brianegan.com/i-heart-node-js/#comments</comments> <pubDate>Wed, 10 Mar 2010 06:20:04 +0000</pubDate> <dc:creator>Brian</dc:creator> <category><![CDATA[JavaScript]]></category><guid
isPermaLink="false">http://brianegan.com/?p=35</guid> <description><![CDATA[Node.js is one of the most intriguing projects I&#8217;ve seen in a while. For some reason, I&#8217;ve never truly enjoyed programming until I started programming in JavaScript. I did the usual BASIC programming as a freshman in high school, and actually tried learning C a bit earlier when I was 12 (let&#8217;s just say it [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://nodejs.org">Node.js</a> is one of the most intriguing projects I&#8217;ve seen in a while. For some reason, I&#8217;ve never truly enjoyed programming until I started programming in JavaScript. I did the usual BASIC programming as a freshman in high school, and actually tried learning C a bit earlier when I was 12 (let&#8217;s just say it didn&#8217;t go over well, except that I did win a C &#8220;competition&#8221; in high school based on that knowledge).</p><p>But when I could start hooking up my designs to really cool functionality, such as <a
href="http://code.google.com/p/dmmonocle">dmMonocle</a>, my jQuery plugin for viewing images, I loved the combination that was possible. I could not only make beautiful designs, I could make beautiful web applications. My code could react to the actions of users &#8212; or in JavaScript lingo, the events of users.<br
/> <span
id="more-35"></span></p><h2>Server side JS?</h2><p>I&#8217;ve since learned to branch out, and have an affection for Python as well at this point! But I&#8217;m still in that weird designer/front end dev gray area, so I don&#8217;t get as much Python time as I&#8217;d like. However, with the advent of Node.js, I can now do some pretty awesome server side stuff in my favorite language. That&#8217;s right, you heard me: JavaScript is going server side &#8212; and even further in the future! Here&#8217;s the code I wrote to implement a <em>very</em> simple web server with Node.JS:</p><pre name="code" class="javascript">
var fs = require('fs'),
    sys = require('sys'),
    http = require('http');

// Simple Server Function
// Pulls the url of the request and responds with the appropriate HMTL file
// If no HTML file found in the request url, returns a 404 error. Boosh!

http.createServer(function (req, res) {

    var HTML;

    if(req.url.search(/\.html$/) > -1) { // Searches to see if the URL entered by the user ends in .html
        fs.readFile('../' + req.url, encoding='utf8', function(err, data) { // If it does, it tries to read the file
            if (err) { // Oh noes! The file doesn't exist! 404-butter time!
                _404(err);
            } else {
                HTML = data;
                res.writeHead(200, {'Content-Type': 'text/html'});
                res.write(HTML);
                res.close();
            }
        });
    } else { // Hey, they didn't enter a valid URL! You know what that means -- 404 BUTTER TIME!
       _404();
    }

    function _404(err) {
        if (err) sys.log(err);
        fs.readFile('../404.html', encoding='utf8', function(err, data) {
           HTML = data;
           res.writeHead(200, {'Content-Type': 'text/html'});
           res.write(HTML);
           res.close();
        });
    }

}).listen(8080); // Listen on port 8080
sys.puts("Brian's HTML Server running at http://127.0.0.1:8080/"); // Output a string to STDOUT (the command line in most cases)
</pre><h2>Lame, but WTF is require()?</h2><p>Now, this server isn&#8217;t very durable. For example, the html files have to be one directory down from the node server &#8212; not awesome, and it serves no CSS. But it was just a simple test script I wrote in 5 minutes to see what could be done with Node, and was a heck of a lot of fun. It also introduced me to the <a
href="http://commonjs.org/">CommonJS</a> project, and their implementation of modules. First of all, the CommonJS project aims to produce a common spec for server-side and application-building JavaScript  &#8212; i.e. JavaScript&#8217;s not just for browsers anymore!</p><p>Now on to modules: at the top of the code, you&#8217;ll see I&#8217;m declaring 3 variables, all of which call a <code>require()</code> function. That require function is an example of the CommonJS module implementation, a way for JavaScript to pull in the contents from another file in a similar fashion to Python&#8217;s <code>import</code> or Ruby&#8217;s <code>require</code>.</p><h2>The way of the future?</h2><p>Pretty cool stuff for a few minutes of compilation time, reading through the <a
href="http://nodejs.org/api.html">Node.JS API</a> (which only takes about 45 minutes even if you&#8217;re slow like me), and then getting a little dirty &#8212; and by that, I mean not dirty at all.</p><p>There&#8217;s some real potential here, and I&#8217;m going to take some time to play around with <a
href="http://github.com/visionmedia/express">Express</a>. It&#8217;s a Node.JS web framework inspired by a Sinatra, a Ruby web framework. Sinatra is another intriguing piece of software, and combined with HAML and SASS, two great technologies for rapidly developing HTML and CSS, it&#8217;s a very nice package indeed. Express seems to have adopted those parts of Sinatra, and should be just a grand old time to tinker with.</p> ]]></content:encoded> <wfw:commentRss>http://brianegan.com/i-heart-node-js/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>jQuery DOM Manipulation und Performance</title><link>http://brianegan.com/jquery-dom-manipulation-und-performance/</link> <comments>http://brianegan.com/jquery-dom-manipulation-und-performance/#comments</comments> <pubDate>Wed, 10 Feb 2010 08:00:18 +0000</pubDate> <dc:creator>Brian</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Performance]]></category><guid
isPermaLink="false">http://brianegan.com/?p=19</guid> <description><![CDATA[I&#8217;m not promising this blog is gonna be hot and spicy all the time, but It&#8217;s been a while since I posted, and I thought I would make it something geeky &#8212; performance tests! I swear I&#8217;m working on a new template for this site, and naturally it&#8217;s going to revolutionize web design. Unfortunately, skiing [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;m not promising this blog is gonna be hot and spicy all the time, but It&#8217;s been a while since I posted, and I thought I would make it something geeky &mdash; performance tests! I swear I&#8217;m working on a new template for this site, and naturally it&#8217;s going to revolutionize web design. Unfortunately, skiing and learning to play the Ukulele keeps eating up all my time! But I digress&#8230;</p><p><span
id="more-19"></span></p><h2>Flame war! (Intro)</h2><p>I was actually a sociology major in college, so I kind of enjoy the rigor of trying to test things well. This is not a rigorous test &mdash; but I did learn some fun stuff!</p><p>Over at NETTUTS there was <a
href="http://net.tutsplus.com/tutorials/javascript-ajax/24-best-practices-for-ajax-implementations/">a discussion about the best way to update the DOM using jQuery</a>. The author suggested this method, and was promptly called an idiot:</p><pre name="code" class="javascript">
var updatedText = "<span id="\&quot;elem1\&quot;">Value1</span>
<span id="\&quot;elem2\&quot;">Value2</span>";
$("#container").html(updatedText);
</pre><p>I actually used a variation on this method when writing <a
href="http://code.google.com/p/dmmonocle">dmMonocle</a>, my jQuery plugin which makes viewing images within library digital collections, such as the <a
href="http://digital.library.unlv.edu/http://digital.library.unlv.edu/boomtown/dm.php/snv/1173">Southern Nevada Boomtowns Collection</a>, a bit easier. What dmMonocle does is generate a series of tiles (<code>divs</code>) and then injects them all together at once, just as the author suggested above. Others claimed that the built-in jQuery methods (<code>.html()</code> &amp; <code>.append()</code>) were more readable, and should therefore be used.</p><h2>Test me once, shame on you (Methods)</h2><p>In these cases, I like to let the numbers speak for themselves, and so I created a series of tests using JSFiddle that test the performance of three methods. I posted a couple of <a
href="http://jsfiddle.net/cMHzS/1/">simple tests</a> to NETTUTS this morning, but I thought it would be fun to get more in-depth and play around with loops of console profiles and timers to test performance (you know, how every sane person likes to spend their time)!</p><p>I wanted to time and profile three different functions:</p><ul><li>Build a string of spans and inject it once into a container using the <code>.html()</code> method</li><li>Update each one of the spans individually with <code>$(#span).html("new html")</code></li><li>Building the spans within the contain using the <code>.appendTo()</code> method.</li></ul><p>As the bad joke of a subheading should tell you, we don&#8217;t simply want to run these timers and profiles once, we want to run them 100 times (more would probably be better). To run them once would be a grave mistake. Don&#8217;t you remember doing 10 trial runs in science class to eliminate error? Did you think the cheeky lab report workflow of this post was a mistake?</p><p>I&#8217;m also running these tests on a Summer 2009 Macbook Pro, and would love to hear about results from other machines.</p><h2>Show me the code! (Data)</h2><ul><li><a
href="http://jsfiddle.net/cMHzS/9/">Timer Tests</a></li><li><a
href="http://jsfiddle.net/cMHzS/10/">Profile Tests</a></li></ul><p>As I said, these tests run each method 100 times. Scratch that, Firefox ate it when I tried that =P I&#8217;ll just run em a bunch manually and record numbers the good ol&#8217; fashioned way. I&#8217;ll work on best practices for averaging results in a future post</p><h3>Chrome</h3><p>My new browser of choice:</p><ol><li>First Test Average: 2-3ms</li><li>Second Test Average: 6-8ms</li><li>Third Test Average: 8-10ms</li></ol><h3>Firefox 3.6</h3><p>Tried and true (but slooooooow):</p><ol><li>First Test Average: 11-12ms</li><li>Second Test Average: 21-23ms</li><li>Third Test Average: 37-39ms</li></ol><p>I mean look at that. 3-5x slower than Chrome 4 on simple operations.</p><h3>Safari</h3><p>Safari was&#8230; creepy with how consistent it was. I think I may be doing something wrong</p><ol><li>First Test Average: 3ms</li><li>Second Test Average: 6ms</li><li>Third Test Average: 16ms</li></ol><h3>Internet Explorer</h3><p>I can only run IE on a virtual machine at the moment, and I&#8217;m not sure how reliable that is. Give me some data!</p><h2>Blah Blah Blah, what&#8217;s the bottom line (Results und Discussion)</h2><p>Bottom line is that, according to my tests, using native JavaScript strings to build the html and injecting it once is roughly twice as fast as the updating method, and 3-5x times faster than append method (which is the most similar to the original string function). These results are due to the fact that the string method skips over calling not only the jQuery constructor <code>$()</code> on each iteration, but also the jQuery methods <code>.html()</code> and <code>.append()</code>, which themselves call multiple functions (as demonstrated by the profiling tests).</p><p>Based on those facts, when I need to inject any significant amount of code, I&#8217;m going to try to do as much building as I can in regular old JavaScript, avoiding the DOM and unnecessary jQuery methods as much as possible. Look, jQuery is great and really fast; I mean, I&#8217;m literally talking about 2-5x slower than 2 <em>mili</em>seconds). But, I&#8217;ll basically take any speed advantage I can get, and in this instance there is a clear winner. I personally don&#8217;t think the JavaScript is too much uglier or less maintainable than the jQuery fashion, but that is really a matter of taste. When it comes to the data, there&#8217;s no debate.</p> ]]></content:encoded> <wfw:commentRss>http://brianegan.com/jquery-dom-manipulation-und-performance/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 4/24 queries in 0.003 seconds using disk
Object Caching 350/402 objects using disk

Served from: brianegan.com @ 2012-05-20 02:46:33 -->
