Saturday, 19 July 2008

Timing is everything

Here for your delight.

A simple way to track timing in Javascript.

[cc lang="php" tab_size="2" lines="40"]
// TIMESTAMP Function
function timestamp(){return (new Date()).valueOf();}

// TIMER Function, wraps timing arround function call
function timer(f){
var t1 = timestamp();
for(var i = 0; i < 1000; i++){
f();
}
var t2 = timestamp();
return t2-t1;
}

// The function to test
var testFunc = function(){
var v = (window["DoYouBelieveInSpeedReading"])?true,false;
}
// Now call the timer
alert("time taken="+(timer(testFunc)/1000) + " seconds" );
[/cc]

The core is converting a date object into milliseconds: date.getValueOf(). This will give you a Long of the number of millseconds since 1970.

so var t1 = timestamp(); will store the start time.

Then do the thing you want to test... I would suggest a very large number of times... I needed to make it 100,000 for the Regex below.

and var t2 = timestamp(); becomes the end time.

Now totalTimeTaken = t2 -t1; in millseconds.

Finally time = totalTimeTaken/1000; to get the number of seconds that the function took.

I wrapped it all up in a function timer...

which can be called by
alert("time taken="+timer(myFunc)/1000);


I recently used this to test String matching functions, to find that indexOf is blisteringly fast, followed closely by pattern.test, and then way behind that String.match .

For 100,000 repetitions:
String.match: 1300ms
pattern.test: 360ms
String.indexOf: 180ms

So the moral of todays story is...


  • use indexOf if you can


  • pattern.test if you must have REGEX or more than one match


  • and AVOID String.match if you can


Just remember that premature optimization is a bad thing...

Happy Testing...!

Friday, 18 July 2008

Setting an Exclusion Cookie for Google Analytics with a Bookmarklet

I have been doing a lot of work on my reading software website recently. This has played havoc with my Google Analytics reports.

So I set about looking for a way to block my own hits from the results. Having done a good search for up to date techniques, it seems that a cookie and a filter in analytics are still the current standard.

I came across loads of articles explaining all this, but none were really clear, so I thought I might cover the subject again. Partly for those who follow after me, and partly so I never need to research this again.

So for posterity here’s how to go about it.

Creating the Exclude Filter

I have written a seperate post on how to add an Exclusion Filter to Analytics in 10 easy steps with loads of screenshots.

How to add an Exclusion Filter to Analytics in 10 steps



Creating the Cookie

Rather than go down the route of having to install something onto my website, I decided to create a BookMarkLet to generate the exclusion cookie.

This means I can use it for any site, it is quick and simple to use and most importantly there is no need to install anything on the website itself.

Another benefit is that I can get anyone working on or reviewing my site to use it as well, without too much trouble. All in all, a win-win solution to the problem.

Get the BookMarkLet: Drag this link to your browser bar (add it to your bookmarks)
GA IgnoreMyHits

Then to put the cookie in place:

  • Go to the site you want to stop recording hits on
  • Click on the bookmarklet(above) you have stored
    • let it add the exclusion cookie to your browser.

First : choose you exclusion cookie name...



 Then: Make sure it all worked. It will recognise your analytics install and tell you which one it detected.





If, like me, you use differant browsers for testing, make sure you do this with all of your browser,s rather than just your main one. This will ensure that your analytics results are totally accurate.

The only thing you must ensure is that you use the same name for both the cookie and the exclusion filter in analytics.



So there you go a nice quick simple solution for clearing up your Analytics results.

I would like to thank, Justin Cutroni , as his blog was the first clear explanation I encountered.



A shameless plug..

Bring the benefits of over 15 years experience in internet technologies, from programming through to usability & design, persuasive copy-writing and site speed optimizations.
Check out my consulting company, Technical Magic.

10 easy steps to add an exclusion filter in Google Analytics

I have been doing a lot of work on my reading software website recently, this has played havoc with my Google Analytics reports.

I set about looking for a way to block my own hits from the results, having done a good search for up to date techniques, it seems that a cookie and a filter in analytics are the current standard.

I came across loads of articles explaining all this, but none were really clear, so I thought I might cover the subject again. Partly for those who follow after me, and partly so I never need to research this again.

So for posterity here's how to go about it.

Creating the Cookie

Firstly, I created a bookmarklet to generate the cookie . This is covered in another post.

Remember, you must put the same name into both the cookie and the exclusion filter.

How to add an exclusion Filter to Analytics in 10 steps

I am going to assume you are logged into analytics...

1. Click on Filter Manager





2. Click on Add Filter





3. Enter a name for the filter





4. Choose the Filter Type (select Custom Filter)

5. Make sure the custom filter type is Exclude




6. Choose the Filter Field (Select User Defined)

7. Put your Cookie-Name (See Creating the cookie above) into Filter Pattern




8. Select your site/s from the available profiles. You can use this method for a single site or as many sites as you like that are listed in your profile.

9. Click Add to move your sites to Selected Website Profiles




10. Click Save Changes




That's it, really quite simple. Just make sure you put the same name in the cookie and the Filter-Pattern (Step 7) and everything will be fine.

You have now made sure that your hits will be excluded from your analytics results.

I would like to thank, Justin Cutroni, as his blog was the first clear explanation I encountered.

A shameless plug..

Bring the benefits of over 15 years experience in internet technologies, from programming through to usability & design, persuasive copy-writing and site speed optimizations.
Check out my consulting company, Technical Magic.