Simple function to detect Array in JavaScript

JavaScript doesn’t really separate Arrays from Objects. In other words, if you check typeof for something like { red:44, green:57, blue:220 } or [44, 57, 220], they both return “object”, even though the second one is an Array (the two are similar but not exactly the same, for example, you can’t access the “red” value of the object via index 0). So I needed a quick way to detect array as opposed to an object and came up with this short function.

function istype(obj) {
	return (typeof obj !== 'object' || typeof obj[0] === 'undefined')
		? typeof obj
		: 'array';

GitHub Mark istype.js gist on GitHub

Short jQuery script to clear form

Clearing a form can sometimes be a bit of a hassle since there’s no native functionality for that – the “reset” button resets the form fields to their default values, not clears them. The script below takes care of exactly that, taking into account some of the newer input types (such as “email” and “date”). It is written as an extension to jQuery, meaning you can use it like a jQuery method, something like this `$(‘#myform’).clearForm()`, but it can easily be written as a regular stand alone function.

// .clearForm - clears form fields from any values
$.fn.clearForm = function () {
	var el = this.find('input, select, textarea'),
		len = el.length,
		i = 0;
	for (; i  len; i++) {
		switch (el[i].type) {
			case 'text':
			case 'password':
			case 'select-one':
			case 'select-multiple':
			case 'textarea':
			case 'email':
			case 'date':
			case 'number':
			case 'phone':
			case 'checkbox':
			case 'radio':
				el[i].checked = false;
// end .clearForm

Read more »

Simple Tooltip plugin for jQuery

This is a simple tooltip plugin for jQuery. I wanted to create it in part to try out a few ideas, but also to have something a bit nicer than the default tooltips provided by the browsers. It doesn’t allow for ton of customization (none in fact), although the style can be easily modified via CSS. I like customization so it’s something I may add in the future but for now I wanted to get this going and not get bogged down in a more complex implementation. In the process of writing this latest iteration of the plugin (it’s not the first time I’ve started it) I had a bit of a “lightbulb” moment and I wanted to share it. Read more »

Out with cookies, in with local storage – jStorage and jQuery

HTML5 Local Storage and jStorage

If you still find yourself on occasion using cookies when developing websites – to remember some setting, or selection, or whatever, stop it! Cookies suck – all you can do is store simple strings, which doesn’t give you a lot of options when you need to store complex information that might require an array or an object.

Local storage, on the other hand, is an HTML5 specification that allows browsers to store objects, XML or JSON data easily and it’s fairly widely supported. Better yet, with the jStorage library it works on pretty much every browser of consequence, including IE7 and IE6, where it falls back to userData behaviour. The only downside in case of IE7 and IE6 is that storage size is limited to 128KB, whereas in most other browsers it’s 5MB. Read more »

Testing string concatenation vs. array join performance in JavaScript

Append String vs Join Array Test Results

Some time ago in an “Optimize jQuery/JavaScript” type of article I read that array joins are faster than string concatenations. It seems that this is not true! Although I didn’t just believe the article blindly. I ran a few quick tests and indeed seemed to get results that supported that conclusion. This however, was a while back, it’s possible I screwed up the tests, or maybe browsers started doing things differently.

I’ve already verified, more recently, that this is not the case but I wanted to setup another test using the jsPerf website that allows one to create public tests that aggregate results from all the testers. They also show the results in a nice chart that gives me a big picture view of the test results. In any case, I created a test with 4 cases to see what kind of an advantage string append has over array join. As  well as compare different ways to append strings. The test is available at Read more »

jQuery 1.6 Released

So I’ve been reading the change log for jQuery 1.6 and it’s a doozy, with tons of great improvements (performance particularly) and some nice changes. There’s been a bunch of performance optimizations to both .attr() and .data() functions, which I use often in my projects, so that is definitely welcomed. Not to mention the more clear separation of attributes and properties for DOM elements, which should get rid of some of the weird behaviour I sometimes see with checkboxes and radios buttons when I manipulate their state with JavaScript. On top of that there are improvements to animation performance and a kitchen sink of other changes. Needless to say I’ll be switching my codebase to 1.6 ASAP.

Optimizing JavaScript and jQuery

I’ve been working on a new project – a web application of sorts. Basically, unlike a regular web site, everything in this web applications occurs on the one page. Moreover, due to the nature of the webapp there’s a lot of javascript code handling everything from keyboard input, through DOM updates and to Ajax requests. While not time critical, it is nonetheless essential to keep this webapp working smooth and fast, and so as part of my development work on it I have been delving deeper into the “science” of optimizing javascript code, and also more specifically optimizing jQuery.

I have always been very performance conscious. It probably comes from my work as PC game developer where a single line of non-optimized rendering code could mean the difference between getting 60fps and 20fps during gameplay. In any case, I really enjoyed discovering some new tricks to squeeze more performance out of my code, and admittedly remembering some old ones as well. So I wanted to share some of those tricks, both the basic, but essential ones and the more advanced techniques. Read more »

Page 1 of 212