Code of the day: Javascript check if an object is empty


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

`isObjectEmpty()` is a function that will only return true if the passed an object that contains no keys.
This is different than jQuery.isEmptyObject() because the passed argument MUST be an object.

/**
* Check to see if an object has any keys.
* @param {Object} obj
* @return {Boolean}
* @author Larry Battle <bateru.com/news>
* @license WTFPL
**/
var isObjectEmpty = function( obj ) {
	var name;
	for ( name in obj ) {
		return false;
	}
	return obj != null && typeof obj === "object";
};

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Code of the Day: Javascript Auto-complete date format MMDDYYYY


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

/**
* This function helps to autocomplete the date format MMDDYYY
* Converts M to 0M and MMD to MM0D. Ex. `1/` to `01/`, `01/1/` to `01/01/`
* Adds slash for MM and MMDD Ex. `01` to `01/`, `01/02` to `01/02/`
* Converts YY to YYYY. Ex. `01/01/01` to `01/01/2001`
*
* @param {String} str
* @return {String}
*/
var autocompleteMMDDYYYYDateFormat = function (str) {
        str = str.trim();
        var matches, year,
                looksLike_MM_slash_DD = /^(\d\d\/)?\d\d$/,
                looksLike_MM_slash_D_slash = /^(\d\d\/)?(\d\/)$/,
                looksLike_MM_slash_DD_slash_DD = /^(\d\d\/\d\d\/)(\d\d)$/;
 
        if( looksLike_MM_slash_DD.test(str) ){
                str += "/";
        }else if( looksLike_MM_slash_D_slash.test(str) ){
                str = str.replace( looksLike_MM_slash_D_slash, "$10$2");
        }else if( looksLike_MM_slash_DD_slash_DD.test(str) ){
                matches = str.match( looksLike_MM_slash_DD_slash_DD );
                year = Number( matches[2] ) < 20 ? "20" : "19";
                str = String( matches[1] ) + year + String(matches[2]);
        }
        return str;
};

Demo

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Re-implementation of Javascript’s Array.prototype.concat()


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

Just for fun I decided to see if I could write a faster version of `Array.prototype.concat()`.

var concat = function (arr) {
	var args = arguments,
	len = args.length,
	ArrayTypeOf = "[object Array]",
	toString = Object.prototype.toString,
	push = Array.prototype.push;
 
	for (var i = 1; i < len; i++) {
		if (toString.call(args[i]) === ArrayTypeOf) {
			push.apply(arr, args[i]);
		} else {
			arr.push(args[i]);
		}
	}
	return arr;
};

Benchmark Script.

console.time("native concat()");
for(var i = 0, len = 2020; i < len; i++) console.log( [].concat( new Array(1000).join("ax").split("") ).length );
console.timeEnd("native concat()");
 
console.time("custom concat()");
for(var i = 0, len = 2020; i < len; i++) console.log( concat( [], new Array(1000).join("ax").split("") ).length );
console.timeEnd("custom concat()");

Benchmark results:

native concat(): 386.000ms
custom concat(): 400.000ms

Well, I got quite close but didn’t surpass the raw power of native code.
Here are some other alternatives to concat but with limited functionality.
http://jsperf.com/concat-vs-push-apply/19

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube