Number Search

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var str = "Hello6 9World 2, Nic8e D7ay!";
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
NS1
function NumberSearch1(str) {
        var chars = str.replace(/[^\w]/g, ""),
            sum = 0,
            total = chars.length;
       
        for(var i=0, e=total; i<e; i++) {
            var pInt = parseInt(chars[i]);
            if ( pInt ) {
                sum += pInt;
                total--;
            }
        }
       
        return Math.round( sum/total );
}

NumberSearch1(str);
pending…
NS2
function NumberSearch2(str) {
        var chars = str.replace(/[^\w]/g, ""),
            nums = chars.replace(/[^\d]/g, ""),
            total = 0;
 
        for(var i=0, e=nums.length; i<e; i++) {
            total += parseInt(nums[i]);
        }
       
        return Math.round( total/(chars.length-e) );
}

NumberSearch2(str);
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