A Textmate Bundle for CSSLint

CSSLint

A quick heads up, if you haven't heard about the new CSSLint project from Nicole ## and Nicholas Zakas, you have been missing out. The project aims to take CSS to the next level and help you understand why your css isn't performing very well. What, you didn't know your CSS isn't performing? Thats because most of us haven't had a good Code Review on most of our CSS.

CSSLint for TextMate

First and foremost, I would like to say, I forked the original jsHint TextMate Bundle. If you don't already have this, you need it. It will change the way you put together your JavaScript, just as I suspect CSSLint will do for your CSS.

I talked with Nicholas at the Velocity conference and decided I would put together a quick CSSLint TextMate Bundle that could make use of the Node Module for CSSLint. After a couple of hours playing with the way TextMate Does its bundles, I was able to get a working product. A Couple of quick emails back and forth and we have a Beta version of the CSSLint TextMate Bundle.

(continue reading...)

Parallel Downloading HTML Assets

Question: Image - inline Script - image, are the images parallel?

Souders just posted the following tweet.

Someone out in #webperf land should test how inline scripts block downloads - eg, image - inline script - image - are the images parallel?

So here is my response to the question...

(continue reading...)

Getting A Class From a DOM Element

Lately, if I am searching for a way to do something in Javascript the only results I am getting ways of doing the procedure using jQuery. While, its true jQuery is pretty much the de facto standard for writing JavaScript anymore, understanding the underlying technology is a must. As well as alot of times, I don't need to load jQuery on the page just to get a DOM Element or its class.

So I recently went looking on how to get class from element in JavaScript. Turns out most of the results were for getting elements by class name, rather than actually getting the class of the element I already had.

Here is how you get a class name from an Element using JavaScript.

	document.getElementById("tag_cloud-2").className

	Output:
		"sidebaritem widget_tag_cloud"

Now, that wasn't so hard. Lets take a look at a performance test: jQuery versus getting a className.

Here is the setup for the test

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

<div class="wrapper">
	<div class="innerWrapper">
		<div id="getMe" class="button">
			<a href="javascript:void(0)" id="button" class="buttonRef">
				<span class="buttonText">Text</span>
			</a>
		</div>
	</div>
</div>

<script>
	var jElem = $("#getMe");
	var dElem = document.getElementById("getMe");
</script>

Here are the tests:

1. jQuery - get Element + get Class   $("#getMe").attr('class');
2. GET DOM ELEM + get Class           document.getElementById("getMe").className;
3. jQuery get Class                   jElem.attr('class');
4. DOM get class                      dElem.className;

Chrome 11 Results

In Chrome using a cached element, getting the classname is 48x faster than using jQuery.
In Chrome getting the element and then getting the classname is 25x faster than using jQuery.

FF 4.0 Results

In FF using a cached element, getting the classname is 65x faster than using jQuery.
In FF getting the element and then getting the classname is 83x faster than using jQuery.

Safari 5.0 Results

In Safari using a cached element, getting the classname is 30x faster than using jQuery.
In Safari getting the element and then getting the classname is 35x faster than using jQuery.

Summary

Whenever possible, using the browsers built in methods will always be faster, and just because you have a library available doesn't mean you should be using it for every DOM query. The speed advantage alone should dictate which methods of the library you are using and when.


Lazy Loading Functions

The best part about this Javascript pattern is the ability to calculate once and then use the new function definition from that point forward.

During the course of building web apps, we usually end up with functions to do certain tasks. However, since those tasks sometimes need to be done differently in each of the browsers, we end up with large if else statements to get the job done.

This is where Self Defining Functions come in handy.

(continue reading...)

jQuery ready, delegate, live!

Recently I was browsing through the jQuery subr reddit and noticed this question pop up. Since I am sure this is a pretty common question on "Why won't my jQuery command work properly", I figured I would give a shot at answering it.

Hey guys, I'm sure this has been answered before but I can't seem to find anything by searching Google or /r/jquery. I am a beginner with this so forgive me, but I can't find an explanation to this anywhere and I'm curious. When I try to stop the default submit function of a form in the <head> tag of a .html file it won't work, but when I copy and paste the same code into the <body> it works fine. Regardless of where I place it, it is always below the link to the jquery library. This is really confusing and I was hoping you guys could explain this for me. Thanks! via jQuery <script> in <body> or *lt;head>.
(continue reading...)

Patterns for enforcing “new”

Recently I was looking around for a book on JavaScript Patterns. I decided to give the JavaScript Patterns by Stoyan Stefanov a try. If you have been looking for a book on Javascript Patterns, I highly recommend this book.

In the 3rd chapter, Literals and Constructors, I noticed this section on "Patterns for enforcing new" with the following entry:

When your constructor has something like this.member and you invoke the constructor without new, you're actually creating a new property of the global object called member and accessible through window.member or simply member. This behavior is highly undesirable, because you know you should always strive for keeping the global namespace clean.
(continue reading...)

Closure Versus Prototypal Object Creation

I was just looking through a new blog post from Brendan Eich over on AMinuteWithBrendan where he went into some detail on Closures versus Prototypal Patterns.

After listening to his pod cast, (you can find the transcript here), I decided to put the two patterns to test. So I fired up a new test on JSPerf.com

(continue reading...)

Writing and Updating DOM Elements in a Frame

Handling writing and updating objects in an iframe can be bothersome in most browsers. Sure things like jQuery tend to help, but not always.

A normal way to access a frame in jQuery is below:

$("#FRAME").contents()

Now, lets say you actually want to get another element inside of the frame, lets get the HEAD element.

$("#FRAME").contents().find("HEAD");
(continue reading...)

Expanding the jQuery Plugin Development Pattern

Doing research on a recent project, I came across an article by Mike Alsup on a "jQuery Plugin Development Pattern". If you haven't seen it already its worth a trip to learningjquery.com to checkout the implementation of his pattern.

"There are a few requirements that I feel this pattern handles nicely:"
  1. Claim only a single name in the jQuery namespace
  2. Accept an options argument to control plugin behavior
  3. -Provide public access to default plugin settings-
  4. Provide public access to secondary functions (as applicable)
  5. Keep private functions private
  6. Support the Metadata Plugin
  7. Allow for extending the object via callbacks (either hooks or events) as applicable
  8. Scope all selectors with the 'this' object

(continue reading...)

jQuery 1.5 Deferreds

Eric Hynds on his blog the other day gave a very good run down of jQuery 1.5 Deferreds. From the post:

Deferreds, new in jQuery 1.5, decouple logic dependent on the outcome of a task from the task itself. They’re nothing new to the JavaScript scene; Mochikit and Dojo have implemented them for some time, but with Julian Aubourg’s AJAX rewrite landing in 1.5, deferreds in jQuery was the logical next step. With deferreds, multiple callbacks can be bound to a task’s outcome, and any of these callbacks can be bound even after the task is complete. The task in question may be asynchronous, but not necessarily.

(continue reading...)


  • JC Fant IV

    About

    JC Fant IV is a Software Engineer at Amazon.com. He has been working as a Web Developer for the last 10 years. Currently he is diving into performance and other technologies around JavaScript.

    Current Projects

    Currently I am working on test for JSPerf and what it means for Javascript in General. Many things we think we know to be true, may in the end be incorrect. Do this, not that!

  • JavaScript Array Example: Creating a Two-dimensional Array
  • © 2012 Scriptble. All rights reserved.