splitwords

JavaScript performance comparison

Test case created by split words

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
regex
var example = "This is an exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxample string.";
var result = splitString(example, 8);
console.log(result);

function splitString(str, length) {
  var regex = new RegExp("/(\w{" + length + "})(?=\w)/g");
  return str.replace(regex, "$1 ");
}
pending…
handmade
var example = "This is an exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxample string.";
var result = splitString(example, 8, " ");
console.log(result);

function splitString(str, length) {
  var words = str.split(" ");
  words = words.map(splitWord.bind(this, length));
  return words.join(" ");
}

function splitWord(length, word) {
  var result = [],
    i = 0;
  while (i < word.length) {
    result.push(word.substr(i, length))
    i += length;
  }
  return result.join(" ");
}
pending…
handmade 2
var example = "This is an exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxample string.";
var result = splitString(example, 8, " ");
console.log(result);

function splitString(str, length) {
  var words = str.split(" ");
  for (var j = 0; j < words.length; j++) {
    var l = words[j].length;
    if (l > length) {
      var result = [],
        i = 0;
      while (i < l) {
        result.push(words[j].substr(i, length))
        i += length;
      }
      words[j] = result.join(" ");
    }
  }
  return words.join(" ");
}
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