string split

JavaScript performance comparison

Test case created by houkanshan

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // prepare the string to be splited
  var string = '';
  for(var i = 0; i < 1000; i++){
      string += 'a,';
  }

};

Benchmark.prototype.teardown = function() {
  // do nothing

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
use regex.exec(string)
var splitReg = /[^,]*,/g;
var strRes = '';
var strItem = '';
while((strItem = splitReg.exec(string)) !== null ){
    // do something to avoid optimizations
    strRes += strItem;
}
pending…
use string.split()
var strArr = string.split(',');
var strRes = ''
for(var i = strArr.length; i--; ){
    // just do something to avoid optimizations
    strRes += strArr[i];
}
pending…
use string.match(regex)
var splitReg = /[^,]*,/g;
var strRes = '';

var strArr = string.match(splitReg);
for(var i = strArr.length; i--; ){
    // do something to avoid optimizations
    strRes += strArr[i];
}
pending…
use string.split() with regex1
var strArr = string.split(/(?=,)/);
var strRes = ''
for(var i = strArr.length; i--; ){
    // just do something to avoid optimizations
    strRes += strArr[i];
}
pending…
use string.split() with regex2
var strArr = string.split(/,/);
var strRes = ''
for(var i = strArr.length; i--; ){
    // just do something to avoid optimizations
    strRes += strArr[i];
}
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.

0 Comments