Chunk methods

JavaScript performance comparison

Test case created

Info

See http://stackoverflow.com/questions/10474992/split-a-javascript-string-into-fixed-length-pieces

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = "1234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412"
   
    function A(a) {
        var b = [];
        for(var i = 4; i < a.length; i += 4){ // length 4, for example
           b.push(a.slice(i-4, i));
        }
        b.push(a.slice(a.length - (4 - a.length % 4))); // last fragment
        return b;
    }
   
    function B(a) {
        return a.match(/(.{1,4})/g);
    }
   
    function C(a) {
        var size = 4
        return [].concat.apply([],
            a.split('').map(function(x,i){ return i%size ? [] : a.slice(i,i+size) }, a)
        );
    }
};
</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
Splice, slice
var chunks = A(a);
pending…
Regex
var chunks = B(a);
pending…
Array concat
var chunks = C(a);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment