lines

Benchmark created on


Setup

var line = new Array(80);
line.fill('A');
var lines = new Array(1000000);
lines.fill(line.join(''));
var text = lines.join('\n')

Test runner

Ready to run.

Testing in
TestOps/sec
baseline
    var lineregex = /\n/g;
    var lines = [];
    var position = 0;
    var match;
    do {
      match = lineregex.exec(text);
      var line = {
        line: lines.length,
        start: position
      };
      lines.push(line);

      if (match){
        line.textEnd = match.index;
        line.end = match.index + match[0].length;
        position = line.end;
      }
      else {
        line.textEnd = position + text.slice(position).length;
        break;
      }
    } while (true);
ready
split and map
var position = 0;
lines = text.split(/\n/).map(function(lineText, index){
	var lineObject = {
		line: index,
		start: position,
	};
	position += lineText.length
	lineObject.textEnd = position;
	position += 1;
	lineObject.end = position
});
ready

Revisions

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