Friday, October 7, 2011

jQuery speed test - sibling selector

I'll continue the previous post with a similar one. But this time I tested the sibling selector of jQuery. And as it turned out it is quite interesting but I don't want to kill the joke too early, so let's see that pic:


Well, IE is...slow, so to see things clearer I have removed it from the graph:


Obviously the fastest solution is: jQuery("h1 + h2")

BUT... there are some really interesting things going there. How can it be, that jQuery("h1").siblings("h2") is sooo much slower, than this:

jQuery("h2").filter(function () {
    return jQuery(this).prev()[0].tagName == "H1";
});


If anybody knows the answer, please share it with us!

jQuery speed test - child selector

Selecting child elements with jQuery is not a challange. However selecting it the fastest way is. I did a simple jQuery selector speed test with the latest versions of Firefox, Chrome, Opera and Internet Exporer. The values you see here are in milliseconds per 1000 cycle.

Here is what I got:


Based on the previous chart it seems... well, quite obvious that IE is the slowest and Chrome is the fastest... but returning to the topic... :)

Here are the comparisons of the fastest ways of getting the children elements (the datas from IE are excluded from the test now due to the large difference):

For all child elements the best way is:
jQuery("li > *"). It's:
  • ~29.4% faster than jQuery("li").children()
  • ~86.1% faster than jQuery("> *", li)
  • ~140% slower than li.children()

And for only link elements the best method is:
jQuery("li > a"). It's:
  • ~65.2% faster than jQuery("li").children("a")
  • ~87.5% faster than jQuery("> a", li)
  • ~47.3% faster than li.children("a")

The other thing to observe is that the loops are much faster if you store the base jQuery wrapper object in a variable.

Saturday, August 20, 2011

Javascript - pausing script flow ( sleep )

Sometimes it happens, that I need to PAUSE the flow of my script. And I mean pause, with capital letters, because these times a simple setTimeout would not help.

There is a very nice, browser independent method to do this:

1. Create a synchronous AJAX call
2. On the server side call the "sleep( [sec] )" function

To speak in code, this is the javascript (jQuery) part of it:

function pause(millisec) {
	jQuery.ajax({
		type: "GET",
		url: "index.php",
		data: {
			task: 'pause',
			pauseMillisec: millisec
		},
		async: false
	});
}


And this is the PHP part:

<?php 
if( $_GET["task"] == "pause" ) {
	sleep( $_GET["pauseMillisec"]/1000 );
} 
?>

That's all folks.. isn't it easy? :)

Feel free to share your thoughts with me.

Sunday, March 20, 2011

jQuery broken picture fixer

Description: fixes and handles pictures which are not found.

During website development there are always pictures which can not be found. In Firefox these pictures are automatically hidden, but for example in Internet Explorer they are marked with a little image with a red “X” in it (which is quite ugly by the way). It's better to show your own “nopic” picture, which suits to your own design and taste. Furthermore, when you have to alter your layout if a picture is not found, you can handle it client side with this jQuery plugin.

What is this good for? ... Well, you can:
  • mark the broken images on your site the way you like it
  • replace automatically these images to a default “nopic” picture
  • define a custom callback function, if a picture is not found
  • solve the tasks above with a jQuery plugin with a very small footprint

“Nice...this is very useful! But how can I use it?”

It's pretty simple.

Basic usage: Default “nopic” image is “images/nopic.jpg”
1
jQuery(".picture").picfix();

Default image configuration: With the “altImg” parameter.
1
2
3
jQuery(".picture").picfix({
    altImg: "images/mynopicture.jpg"
});

Error handling configuration: With the “onerror” parameter.
1
2
3
4
5
jQuery(".picture").picfix({
    onerror: function () {
        // do something...
    }
});

Files:
  • jquery.picfix.min.js (~0.53kb)
  • jquery.picfix.compressed.js (~0.58kb)
  • jquery.picfix.js (~0.79kb)
  • usage.js

Download:

Feel free to write me a comment, and if you like this little plugin please contribute my efforts.

Tuesday, March 15, 2011

Taskfreak! Desktop: the TaskFreak! desktop gadget

What is TaskFreak?

TaskFreak! is an open source web based task management software. It's very handy, uncomplicated and it can be learned quickly. It has all the requirements nessessary to manage tasks fast and effective.

And what is this desktop application?

This is a Windows gadget, that allows you to be immediately informed about the changes in your TaskFreak!'s task list. In addition, you don't have to be continuously logged in in a browser, since you can see and manage every ongoing task directly on your desktop.

How does this desktop gadget work?

After installing and configuring - which is described later - the application is in observer mode. This means, it's just checking periodically, if there are updates in your tasks. So if someone (other than you) creates a new task, writes a new comment or changes a task's status, you are informed almost instantly.

You can switch to extended view, by clicking the "bigger size" Windows-default-button on the right top of the gadget. In this view, all the unfinished tasks are listed under each other. This design structure is very similar to the web based TaskFreak!. The colorful squares on the left are the priority and the environment indicators. Next to them is the comment count button, project name and the status buttons. Below these on the left side is the task title and the deadline date on the right side. If you click on the title the full description appears.
You can even read comments, and write new ones, by clicking on the comment count icon.
To get a more detailed picture about the interface click the images below.

User interface explanation



TaskFreak! Desktop in action:


How to install TaskFreak! Desktop?


The installation is really easy. Just follow these steps:
  1. place the desktop.php file in your TaskFreak! root web folder.
  2. run taskfreakDesktop.gadget file. This is a simple archive file and all the sidebar enabled Windows systems are recognising this extension.
  3. on the settings page, type in the URL to your desktop.php. Enter your username and password to your TaskFreak! account. And make some other changes if needed.

That's it. Now you are ready to use TaskFreak! Desktop.

I hope this gadget is at least as useful for you as it is to me. Feel free to write a feedback, or request a new feature. I have some plans for further development...but I welcome any ideas, especially if they are combined with donations :)

Files:

  • desktop.php
  • taskfreakDesktop_0.1.2.gadget
  • CHANGELOG.txt
  • LICENSE.txt

Downloads:


Please donate to keep development active!

Thanks for blueBros LLC for providing this awesome design :)



Changelog

  • 0.1.2
    • Improved design, by blueBros LLC
    • Added alert volume slider in settings
    • Added updating interval input in settings
    • And fixed some minor bugs
  • 0.1.1
    • Fixed the "notask" bug
    • Fixed the "empty comment form submitted" bug
    • Logs and errors are from now on shown in the same panel
    • And fixed some minor bugs
  • 0.1.0
    • Listing unfinished tasks
    • Status changing ability
    • Hidden but recallable description
    • Listing task's comments
    • Opportunity to write a new comment
    • Smaller view (and also an extended view)
    • Observer functionality. Alert on:
      • New tasks
      • New comments
      • New status changes
    • Set custom sound alert
    • Shows changes in tasks

Saturday, March 12, 2011

PHP: Your exception classes

Exceptions

This article is the second part of PHP Exception articles. In the previous one, I've written about Exceptions, I've showed you the basic usage and I've provided some examples. It won't be different this time, but I'm going to be a little more advanced. This article is about how to write your own classes.

Reminder: Exception is a special condition that changes the normal flow of the code.


Extending exceptions

You can extend the core exception class in PHP to have your own class which will suit for the given tasks ahead. It's a good way for categorisation of errors. What I mean is, you can make input exceptions, error exceptions, email exceptions. Another example is if you use the MVC programming pattern, you can make classes related to each layer as follows: ModelException, ViewException, ControllerException. After this, you can extend more classes depending on the occuring errors in each layer.

But let's return from programming patterns to the example code below. Take a look at it. :)

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

 class InputException extends Exception {};

 class SomethingBadException extends Exception {};

 class SomethingReallyBadException extends Exception {};


 // ...

 class foo {

  // ...

  function let_it_happen($input) {

   if( !validate_input($input) ) {

    throw new InputException('Invalid input given.');

   }


   if( !calculate_something($input) ) {

    throw new SomethingBadException('Something bad happenned.');

   } else
   {

    if( !generate_something($input) ) {

     throw new SomethingReallyBadException('Something really terrible happened.');

    }

   }

   // ...

  }

  // ...

 }


 // later in the code...

 $bar = new foo();

 try {


  $bar->let_it_happen();


 }
 catch (InputException $e) {

  echo 'Input error: '.$e->getMessage();

  // do something here, like logging
  log('Error: '.$e->getMessage());

 }
 catch (SomethingBadExcpetion $e) {

  echo 'Error: '.$e->getMessage();

  log('Error: '.$e->getMessage());

  handle_some_stuff();


 }
 catch (SomethingReallyBadException $e) {

  echo 'Fatal error: '.$e->getMessage();

  log('Fatal error: '.$e->getMessage());

  $email->notify_admin('www.site.com - problem',"The following thing happened: ".$e->getMessage()."\n Log in immediately.");

 }


?>

I've defined three new exceptions, and I've also written a class where these exceptions appear. Notice the multiple catch blocks. You have to catch all kind of exceptions by making catch blocks for each of the classes. If an exception occurs you can make the necessary steps in each of the blocks to set variables, warn the user, log or notify someone.

You can also add your custom properties and methods to an extended class. But be careful with this! I say this because if you add a method to your exception class that uses an object - for example email - and you throw this exception in a context where that object is not available or currently not loaded, you're shot and you can start debugging and hacking. Please, prevent this and design your application correctly. The code:

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
43
44
45
<?php  
 
 class BadBadException extends Exception {
  
  private $__variable;
  
  function set_variable($in) {
   
   $this->__variable = $in;
   
  }
  
  function get_variable() {
   
   return $this->__variable;
   
  }
  
  function handleBadException() {
   
   log($this->getMessage());
   
   do_something_necessary();
   
  }
  
 }
 
 // ...
 
 try {
  
  throw new BadBadException('bad bad message');
  
 } 
 catch (BadBadException $e) {
  
  echo $e->getMessage();
  
  $e->handleBadException();
  
 }
 

?>

Final words

Deciding to use exceptions is a good start to make your application more logical, more understandable and what is much more important, more flexible.

Zsolt Szivák

Monday, March 7, 2011

Javascript: validating object interfaces

The interface is one of the most used tool in object-oriented programming. Based on the Gang of Four’s Design Patterns book, it is the first principle of reusable OOP. Unfortunately Javascript can’t implement any interfaces. At least not the usual way. And that’s where my interface class comes in. :)

I got the basic idea from the book Pro Javascript Design Patterns [Apress]. The authors have mentioned three types of emulating an interface in Javascript.

  1. Comments:
    "The easiest and least effective way of emulating an interface is with comments. Mimicking the style of other object-oriented languages, the "interface" and "implements" keywords are used but are commented out so they do not cause syntax errors."
  2. Attribute checking:
    A bit better, but it can be easily tricked out by a careless programmer. In this method when creating a class, it is left to the programmer's conscience to decide which interface is implemented. The interface name is stored in an array. Then, when an interface check is required, a function is validating the content of this array. If it says the class is implementing the given interface, the function believes it and the program flow continues.
    Personally, I don’t really like this method.
  3. Duck typing:
    This is where I have had my inspiration :) There is a line in the book, I'm always smiling when I'm reading it: "Duck typing was named after the saying, 'If it walks like a duck and quacks like a duck, it's a duck.' " :)
    Well, I was quite satisfied with this solution. However the example, that the authors were providing is only checking, if the object has the given methods defined.

My improved interface class:

So I decided to improve this Duck typing method, and add some additional functionality. With my interface class, instead of just checking the methods of the class (or object), we can:
  • check variable and method existence
  • check method’s parameters
  • check method return value
  • check variable value
  • define callback error handling function
  • and we can even define own interface functions, to validate the internal state of the object.
It's quite exciting :)

Here is a brief explanation, of how is this class used:
1.) Define an interface:
1
2
3
4
5
var exampleInterface = new Interface("example", { 
   /* requirements*/
}, {
   /* functions called on error */
})

2.) Create your object

3.) Validate the object's state: by passing the validated object and the interface objects as parameters
1
Interface.check( /* object to validate */, /* interface object */[...]);


Let's see some comment-explained code. First, here are some example interfaces defined:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// defined interfaces to implement
// checks all these variables. They have to be defined.
var InterfaceVariables = new Interface('InterfaceVariables', {

   // checks, if it is true
   isBooleanVal: true, 

   // just check the variable if it is defined
   isNullVal: null,

   // this string has to contain this value
   stringVal: 'required string value',

   // variable check with interface function. In this example, a variable has to be greater than one. It must always have a bool return.
   isGreaterThanOne: function(obj) { 
      return obj.isGreaterThanOne > 1;
   },

   // array check. here are other items not allowed
   allowJustThese: ['item1', 'item2', 'item3'],

   // array check. other items are allowed
   theseAreRequired_butOthersAllowedToo: ['item1', 'item2', 'item3', ".."],


}, {
   // this object contains the error handling functions

 
   // this runs only if the isBooleanVal is not true
   isBooleanVal: function (intf, obj) {alert("isBooleanVal is not true. Required interface: "+intf.name+".");},

   // and this runs only if the stringVal value is not the required one.
   stringVal: function (intf, obj) {alert("stringVal value is not valid. Required interface: "+intf.name+".");}

});

var InterfaceMethods = new Interface('InterfaceMethods', {

   // checks if method has only these two params. if others are also enabled, put ".." in the end
   // ret: boolean      checks method's return value. Params: (expected return [, arg1, arg2, ...] )
   methodParamCheckAndReturnCheck: {
      args: ['num1', 'num2'], 
      ret: [12, 2, 3] 
   },

   // i think this is clear
   // method must not have parameters
   // ret: true      expected return value is 6
 methodNoParamAndReturnsSix: { 
      args: [],
      ret: 6
   },

   // checks if this is a function plus the parameters
   checkMethodParams: ['arg1', 'arg2', '..'],

   // just check function existence
   isFunctionDefined: null,

   // no parameters allowed
   isFunctionWithNoParams: [] 

},{
   methodNoParamAndReturnsSix: function (intf, obj) { alert("Error with method: methodNoParamAndReturnsSix. Interface: "+intf.name+"."); }
});


For example in the first interface I defined only variable's, and in the second one the methods' rules. Staying at the example, here is a class which "implements" these interfaces:

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
// class that has to implement the interface
var classToImplementInterface = (function() {

   // this variable should be true, so the interface callback function will be called
   this.isBooleanVal = false;
   this.isNullVal = null;
   this.stringVal = "required string value";
   this.isGreaterThanOne = 2;

   var privateVar = 'private'; // just for fun... (a class' interface is its public variables and methods, if you wouldn't know that :) )
   var privateMultiplyer = 2;

   this.methodParamCheckAndReturnCheck = function(num1, num2) {
      return num1 * num2 * privateMultiplyer;
   }

   // this method should return 6. So this will generate an error.
   this.methodNoParamAndReturnsSix = function() {
      return 5;
   }

   this.allowJustThese = Array('item1', 'item2', 'item3');
   this.theseAreRequired_butOthersAllowedToo = Array('item1', 'item2', 'item3', "item4", "item5");

   this.checkMethodParams = function (arg1, arg2, arg3) {}

   this.isFunctionDefined = function ( can, have, params ) {}

   this.isFunctionWithNoParams = function () {} // must not have params


 
})


And here is, how you can evaluate the rules:

1
2
3
4
5
6
7
/////////////////////////////////////////////////////////////////////
//       CONCRETE OBJECT to IMPLEMENT INTERFACES
/////////////////////////////////////////////////////////////////////
var concreteObject = new classToImplementInterface();

// before doing something validate object's interface
Interface.check(concreteObject, InterfaceVariables, InterfaceMethods);


A concrete example:

Let's say, there is a car class. A method is called which is responsible for moving objects on the canvas, and the car object is passed in as a parameter. However, to move that car, there are some object requirements. For example, is someone driving it? Or is the door closed?. These can be solved with variable checking. Then…has the given car object all the methods which are used later on? Or is the body state higher than 10%? This can be solved with an anonym function. These requirements must be satisfied for error-free run.


Well that's it. I'm quite satisfied with the results. It's small and simple. Probably it will be used only in development phase, so I haven't done any speed optimization.

If you are interested in further development, please let me know!

Source files:

  • index.html
  • interface.js
  • interface.min.js
  • functions.js

Download:




I'm very enthusiastic and appreciate every donation. I will spend the full amount on books, to improve my skills and create more projects like this (and even better).

Thanks,
Phil



Changelog


  • 1.1:
    • Added callback functionality
    • Callback parameters: ( interface object, checked object )
  • 1.0:
    • variable and method existence
    • method's parameters
    • method return value
    • variable value
    • and we can even define own interface functions, to validate the internal state of the object.