String addition versus accumulation

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function subPathAdd(path, n) {
        return (path === '.') ? path + n : path + '.' + n;
    }
   
    function subPathAcc(path, n) {
        var ret = path;
   
        if (path !== '.')
            ret += '.';
   
        ret += n;
   
        return ret;
    }
   
    function subPathAcc2(path, n) {
        var ret = path;
   
        ret += (path === '.' ? '' : '.');
   
        ret += n;
   
        return ret;
    }
};
</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
Addition
var str1 = subPathAdd('.', 2);
var str2 = subPathAdd('.5', 3);
pending…
Accumulation
var str1 = subPathAcc('.', 2);
var str2 = subPathAcc('.5', 3);
pending…
Accumulation with ternary instead of if
var str1 = subPathAcc2('.', 2);
var str2 = subPathAcc2('.5', 3);
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