Posts Tagged ‘memory’

How to Delete Cookies?

Tuesday, July 29th, 2008

One thing you may have noticed if you have started writing Javascript to use cookies is that there is no actual delete command that can be used to delete a cookie after you have created it. This doesn’t mean that you can’t delete cookies, it just means that you need to understand how cookies work in order to be able to control when the system will delete them for you.

When you create a session cookie it will continue to exist for as long as the browser remains open and will be deleted as soon as the browser is closed. This is because session cookies are actually retained in memory by the browser and are never actually stored anywhere. You do not specify an expiry date when creating a session cookie.

If you want a cookie to last for a longer (or shorter) time than you get wioth a session cookie you need to create a first party cookie instead. With a first party cookie the cookie is actually stored in a file on your visitor’s hard drive. You specify an expiry date/time when creating a first party cookie that defines how long the cookie is to be retained on the hard drive. The cookie isn’t necessarily deleted when that date/time is reached but cookies that have passed their expiry date/’time are ignored and so as far as the browser is concerned they don’t exist.

So how does this help us if we decide that we need to delete a cookie befre the date/time that it is set to expire? Well the solution is quite simple, we change the expiry date of the cookie so that it will be considered to have already expired. Rather than having to remember what to do each time, let’s just create a small function for deleting whichever cookie that we want.

function del_cookie(name) {
document.cookie = name +
‘=; expires=Thu, 01-Jan-70 00:00:01 GMT;’;
}

Now all we need to do is to call this del_cookie() function passing it the name of whatever cookie it is that we wish to delete. The function will update the expiry date on the cookie to one long in the past so that the cookie will be considered to be expired and will be ignored by the browser exactly the same as if it didn’t exist.

Why choose that particular date for setting the expiry date to delete the cookie? Well it just happens that all of the date processing within Javascript sees the 1st January 1970 as its starting date and actually records all date/times internally as the number of milliseconds from midnight on that day. Using that date therefore is effectively equivalent to setting the expiry to zero which means that the cookie will be deleted even if your visitor has the date on their computer set incorrectly. Were we to choose a more recent date it would be possible (although very unlikely) that one of our visitors might have their computer date set incorrectly to one earlier than the exipry that we chose and so our attempt to delete the cookie wouldn’t then work for them.

Tips for optimizing your php code

Saturday, June 28th, 2008

# If a method can be static, declare it static. Speed improvement is by a factor of 4.

# echo is faster than print.

# Use echo’s multiple parameters instead of string concatenation.

# Set the maxvalue for your for-loops before and not in the loop.

# Unset your variables to free memory, especially large arrays.

# Avoid magic like __get, __set, __autoload

# require_once() is expensive

# Use full paths in includes and requires, less time spent on resolving the OS paths.

# If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()

# See if you can use strncasecmp, strpbrk and stripos instead of regex

# str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4

# If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.

# It’s better to use select statements than multi if, else if, statements.

# Error suppression with @ is very slow.

# Turn on apache’s mod_deflate

# Close your database connections when you’re done with them

# $row[’id’] is 7 times faster than $row[id]

# Error messages are expensive

# Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.

# Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.

Firefox:Improved Memory Management

Friday, June 27th, 2008

With all new management functions in place, Firefox 3 keeps memory usage under control. The XPCOM cycle collector continuously cleans up unused memory. Plus, hundreds of memory leaks are now remedied.

Firefox 3 Release Candidate 2 released

Monday, June 16th, 2008

Firefox 3 Release Candidate 2 is available in more than 45 languages as a public preview release intended for developer testing and community feedback. It includes new features as well as dramatic improvements to performance, memory usage and speed.