Smart Needle Searching

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Loop over each character
String.prototype.smartNeedleSearch = function(needle){
        var haystack = this.toString().toLowerCase();
        needle = needle.toLowerCase();
        for (var i=0,j=0,c; i < needle.length, c=needle[i]; i++,j++){
                if( ( j=haystack.indexOf(c,j) ) === -1)return false;
        }
        return true;
}

"Lorem ipsum dolor sit amet, consectetur adipisicing elit.".smartNeedleSearch("lor con")
ready
Regex #1
String.prototype.smartNeedleSearchRegex = function(needle){
        var haystack = this.toString().toLowerCase();
        var regexp = new RegExp(needle.split('').join('.*'), 'gi');
        return haystack.match(regexp) ? true : false;
}
"Lorem ipsum dolor sit amet, consectetur adipisicing elit.".smartNeedleSearchRegex("lor con")
ready
Regex #2
String.prototype.smartNeedleSearchRegex = function(needle){
        var haystack = this.toString().toLowerCase();
        var regexp = new RegExp(needle.split('').join('.*?'), 'gi');
        return haystack.match(regexp) ? true : false;
}
"Lorem ipsum dolor sit amet, consectetur adipisicing elit.".smartNeedleSearchRegex("lor con")
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.