String streamer (v2)

Revision 2 of this benchmark created on


Setup

let str = "";
for (let i = 0; i < 1e7; i ++) {
	str += "G1 X10 Y10 Z10 E10.01\n";
}

Test runner

Ready to run.

Testing in
TestOps/sec
str.split
for (const ln of str.split("\n")) {
	//
}
ready
string yield
function* stream(str, splitChar) {
	let part = "";
	for (let i = 0; i < str.length; i ++) {
		const char = str[i];
		if (char === splitChar) {
		  yield part;
		  part = "";	
		} else {
		  part += char;
		}
	}
}

for (const ln of stream(str, "\n")) {
	//
}
ready
string-slice yield
function* stream(str, splitChar) {
	let lastLineIndex = 0;
	for (let i = 0; i < str.length; i ++) {
		const char = str[i];
		if (char === splitChar) {
		  yield str.slice(lastLineIndex, i);
		  lastLineIndex = i;
		}
	}
}

for (const ln of stream(str, "\n")) {
	//
}
ready

Revisions

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