Which selector is faster?

29 Sep

If you have any concerns about performance of your web application, you probably should read this. Some times ago i read on a site (don’t remember which, was like AGES ago) that a good performance improvement is to be VERY specific when you use a library to select an element from the DOM. That article was written with Prototype in mind but i’ve though that will work same for jQuery as well. HUGE MISTAKE!

Kinda obviously, jQuery is NOT Prototype and they seems to NOT work in the same manner. Today i think to test the results of various selector. For this, i used Profiler from Firebug (what? you don’t know what firebug is?). And i was amazed. And i mean AMAZED!

I made this test right on this blog. I used jQuery('li') in console and, after that i used jQuery('ul li') (there is 24 LI elements on the first page). Until today i think the second option is faster. Why? Well.. Is more specific, isn’t it?

The second pick is around 2.6 times SLOWER than first one. (0.533ms vs 0.205ms).

Unfortunately, i’ve try to use the slower technique on many projects. But, lucky me, for no reason, i always forgot to do that (and i use to remember too late) :D

So, be careful! Don’t make this mistake ;) If somebody will tell ya that a method is faster, DON’T take his word as a law. TEST IT!

Tags: Comments (2)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines

One development trick

28 Sep

Actually… There is more than one :D

Keep only one copy of your favorite javascript library (and also up to date!)

I use jQuery a lot. No, actually i LOVE jQuery (is small, is fast, is easy to use, you know, usual craps that make you to love it; just like a woman :D ). To avoid duplicates of jquery, i use a folder called assets (but you can rename as you want as well). Here i keep latest version of jquery and jquery UI 1.5.2 (which now is the last stable version). With wget for windows in the assets folder, i wrote a small script to get latest version:

del jquery-latest.min.js
wget http://code.jquery.com/jquery-latest.min.js

Put this in a file called update.jquery.bat and double click it whenever you want to update your version of jquery (or any other library). This way you can keep all your sites up to date :D BE CAREFUL! Some plugins may WON’T work on newer versions of jquery!

Use snippets like a smart guy!

If you use E-texteditor (and if you don’t, then YOU SHOULD!), you probably know what snippets are. But, the question is: are you using them?

I put some snippets all together into an archive HERE. What you should do with this? Go to Start-> Run (or press WIN+R) and write this: %appdata%\e\Bundles then press enter. Extract the archive content right there and…. Done.

In each HTML document you can write ui then press tab. VOILA! A menu with jquery UI components are displayed and waiting for you to pick from there! Ofcourse, you MUST have UI files (minified) in /assets/ui folder (which must be in the root of your project folder). Again, be careful when you update UI. Even though UI 1.6 have same files as UI 1.5.2, there is (still) some issues with 1.6 (actually it’s a beta).

Tags: , Comments (2)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines

A VERY nice Modal box

26 Sep

How many times did you need a light modal box? To display an error mesage, a picture or even another web page inside of the current page? And every single time, you ended with using a large-full-of-useless-options script. Yeah, i know the feeling. Seriously, i know it VERY well. That’s why i wrote this piece of code to display modal boxes :) Anyhow, this works BEST with AJAX content ;)

UPDATES: known issues

If you need to display more content than fit into a screen height, you won’t see the content after the fold. To fix this, just remove the scroll bind on document:

(document).bind('scroll', function(){
		$('#ntz_modal').css({
			top:$(document).scrollTop()
		});
	});

How to use?

Just call with modalBox('content', 200) where content is what do you want to display (which can be an ajax result) and 200 is the width of the modal box. If you don’t specify any width, the default value (350) is used.

How to close the modal box?

Well… Just call the function modalBox(). When no parameter is passed, then the modal goes off. Nice, isn’t it? :D

Browser compatibility

The script was tested in FF 2, FF 3, IE 6, IE 7, Safari (on Win), Opera 9.5. If you don’t know what these letter means, there is a very high probability to don’t need this snippet ;)

Selectboxes on IE6 - how they are managed?

Very simple: i hide ALL selects when modal is shown and i show them back when modal is destroyed. Ofcourse, you can use bgiframe plugin. Which is

Requirements

The script is tested with last version of jQuery (1.2.6) but should work fine with previous (and ofcourse next) versions. If you use an ancient version, you may need some extra plugins (like dimensions)

What’s the cost?

Prepare you credit card, prepare you paypal account or whatever you want. Because this is totally FREE. You may use, modify and distribute whatever you like. A small link back would be nice (but NOT mandatory).

You bastard! You use your name in modal elements! Yeah, so? No, seriously, i used this way because there is no way to know what div did you use. And the chance to have a div called ntz_modal in your code is kinda.. hmm… Right! ZERO! Of course, you can change these ID’s whatever you like it :)

How do I get the code?

Keep reading!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
function modalBox(content, width, callback) {
	if(!content){
		$('#ntz_modal').remove();
		$('#ntz_overlay').fadeOut(function(){$(this).remove();});
		try{if(IE6){$('body').find('select').visibility('visible');}}catch(err){};
		return false;
	}
	try{if(IE6){$('body').find('select').visibility('hidden');}}catch(err){};
	$('body').append('<div id="ntz_overlay"></div>');
	$('#ntz_overlay').css({
		width		:	'100%',
		height		:	$(document).height(),
		position	:	'absolute',
		left		:	0,
		top		:	0,
		backgroundColor	:	'#FFFFFF',
		zIndex		:	9990,
		opacity		:	0
	}).fadeTo(200, 0.5);
	$('body').append('<div id="ntz_modal"></div>');
	$('#ntz_modal').css({
		border		:	'1px solid #2d7abb',
		width		:	width ? width : 350,
		backgroundColor	:	'#FFFFFF',
		position	:	'absolute',
		left		:	'50%',
		top		:	$(document).scrollTop(),
		zIndex		:	9995,
		marginLeft	:	-(Math.ceil((width ? width : 800)/2))
	}).append(content);
	$('*').blur();
	$('#ntz_modal a:eq(0), #ntz_modal input, #ntz_modal textarea').focus();
	try{
		callback.call();
	}catch(err){};	
	$(document).bind('scroll', function(){
		$('#ntz_modal').css({
			top:$(document).scrollTop()
		});
	});
};


Note: the example is VERY intrusive :D

Tags: , Comments (6)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines

I’ll never sleep again!

20 Sep

No, seriously! This morning, after i woke up i saw this awesome stuff :

- jQuery UI 1.6rc2 is out. Many bug fixes, many improvements, new components (color picker and spinner which… i have no idea what it is for :| )

- Snippely 1.1. Keep your code snippets safe and organized! I use only one AIR application. THIS one.

- Moo Tools 1.2. Despite the fact i’m not a Moo Tools lover, i inform you that the final version is out.

And this just after i opened my feed reader! I feel that today will be a great day :P

Tags: Comments (1)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines

E-texteditor v1.0.30 released

19 Sep

My favorite editor is on version 1.0.30 (launched today). The main feature is that Alexander finally fixed a MAJOR bug: E used to crash frequently when you use remote or SVN. I didn’t have problems with SVN because i never used and frankly i don’t know how to use :P But ftp made me to pull my hair out.

Here are the release notes:

  • Fixed a really nasty memory corruption bug that has been the source of a lot of crashes.
  • Fixed creation of shortcuts in bundle commands.
  • Fixed deletion of bundles via context menu.
  • Fixed crash when clicking on empty dir in ftp project.
  • Fixed webpreview crash.
  • Fixed correct restore of bundle pane state.
  • Fixed bundle env is now also set for CompletionCmds.

Download? As always, on E site: http://e-texteditor.com/download/e_setup.exe

Tags: Comments (0)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines

Older Posts »