word match

JavaScript performance comparison

Test case created by passy

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.1.0/lodash.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.string/2.3.3/underscore.string.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var badMatch = function (theString, thePhrase) {
       return theString.indexOf(thePhrase) !== -1;
    };
   
    var betterMatch = function (theString, thePhrase) {
       var wordRegExp = new RegExp('(^|[^\\w])' + _.string.escapeRegExp(thePhrase) + '($|[^\\w])');
       return wordRegExp.test(theString);
    };
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
indexOf
badMatch("ondon is not even a word", "ondon");
badMatch("But it's a match: ondon", "ondon");
pending…
regExp
betterMatch("ondon is not even a word", "ondon");
betterMatch("But it's a match: ondon", "ondon");
pending…

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

Compare results of other browsers

0 comments

Add a comment