Editing Testing if a string is blank This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var hasTrimRight = !! String.prototype.trimRight; function isBlankWithNativeTrimRight(string) { if (string === '') { return true; } // I assume this is faster, if it exists if (hasTrimRight && string.trimRight() === '') { return true; } // matches most space or empty characters (including some unicode whitespace characters) if (string.match(/^[ \t\r\n\u00A0\u2000-\u200B]+$/)) { return true; } return false; } var hasTrim = !! String.prototype.trim; function isBlankWithNativeTrim(string) { if (string === '') { return true; } // I assume this is faster, if it exists if (hasTrim && string.trim() === '') { return true; } // matches most space or empty characters (including some unicode whitespace characters) if (string.match(/^[ \t\r\n\u00A0\u2000-\u200B]+$/)) { return true; } return false; } function isBlankNoNative(string) { if (string === '') { return true; } if (string.match(/^[ \t\r\n\u00A0\u2000-\u200B]+$/)) { return true; } return false; } var strIsBlank = (function() { if ( !! String.prototype.trim) { return function(string) { if (string === '' || string.trim() === '') { return true; } return false; }; } return function(string) { // matches most space or empty characters (including some unicode whitespace characters) if (string === '' || string.match(/^[ \t\r\n\u00A0\u2000-\u200B]+$/)) { return true; } return false; }; })(); Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code isBlankWithNativeTrimRight(''); isBlankWithNativeTrimRight(' '); isBlankWithNativeTrimRight(' '); // includes unicode character Test 2 Title Async (check if this is an asynchronous test) Code isBlankNoNative(''); isBlankNoNative(' '); isBlankNoNative(' '); // includes unicode character Test 3 Title Async (check if this is an asynchronous test) Code isBlankWithNativeTrim(''); isBlankWithNativeTrim(' '); isBlankWithNativeTrim(' '); Test 4 Title Async (check if this is an asynchronous test) Code strIsBlank(''); strIsBlank(' '); strIsBlank(' ');