string split (v2)

Revision 2 of this benchmark created by houkanshan on


Description

  • test1:use string.split()
  • test2:use regex.exec(string)
  • test3:use string.match(regex)
  • test4:use string.split() with regex1

Setup

// prepare the string to be splited
    var string = '';
    for(var i = 0; i < 1000; i++){
        string += 'a,';
    }

Teardown


    // do nothing
  

Test runner

Ready to run.

Testing in
TestOps/sec
use string.split()
var strArr = string.split(',');
var strRes = ''
for(var i = strArr.length; i--; ){
    // just do something to avoid optimizations
    strRes += strArr[i];
}
ready
use regex.exec(string)
var splitReg = /[^,]*,/g;
var strRes = '';
var strItem = '';
while((strItem = splitReg.exec(string)) !== null ){
    // do something to avoid optimizations
    strRes += strItem;
}
ready
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];
}
ready
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];
}
ready
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];
}
ready

Revisions

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

  • Revision 1: published by houkanshan on
  • Revision 2: published by houkanshan on
  • Revision 3: published by yoni on