Split on first deliminator (v5)

Revision 5 of this benchmark created on


Description

Test the performance of splitting a string only on the first delimitor

Setup

function test_single(path, split) {
	
	let component, last;
	
	while (path[0] === '/') path = path.slice(1);
	
	for (;;) {
		
		last = path;
		
		[component, path] = split(path);
		
		if (!path) break;
	}
}

const test_paths = [
	'/home/test/src/project/deps/libsomething/dist/file.js',
	'//net/path/with/oops///slashes'
];

function test(split) {
	for (const p of test_paths) test_single(p, split);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Split and Join
function split(p) {
	const parts = p.split('/').filter(v => v);
	const first = parts.shift();
	return [first, parts.join('/')]
}

test(split);
ready
Do/While Loop with string
function split(p) {
	let c = p[0]; len = 0;
	
	do {
		c = p[++len];
	} while (c && c !== '/');
	
	const endFirst = len;
	for (len; p[len] === '/'; len++);
	
	return [p.slice(0, endFirst), p.slice(len)];
}

test(split);
ready
Regex
const pattern = /^\/*([^\/]*)\/*(.*)$/;

function split(p) {
	const match = pattern.exec(p);
	match.shift();
	return match;
}

test(split);
ready
For loop with char codes
const SLASH = '/'.charCodeAt(0);

function split(p) {
	let len = 0;
	
	for(let c = p.charCodeAt(0); c && c !== SLASH; c = p.charCodeAt(len)) len++;
	
	const endFirst = len;
	for (len; p[len] === '/'; len++);
	
	return [p.slice(0, endFirst), p.slice(len)];
}

test(split);
ready
Do/While Loop with char codes
const SLASH = '/'.charCodeAt(0);

function split(p) {
	let c = p.charCodeAt(0); len = 0;
	
	do {
		c = p.charCodeAt(++len);
	} while (c && c !== SLASH);
	
	const endFirst = len;
	for (len; p[len] === '/'; len++);
	
	return [p.slice(0, endFirst), p.slice(len)];
}

test(split);
ready
For loop with strings
function split(p) {
	let len = 0;
	
	for(let c = p[0]; c && c !== '/'; c = p[++len]);
	
	const endFirst = len;
	for (len; p[len] === '/'; len++);
	
	return [p.slice(0, endFirst), p.slice(len)];
}

test(split);
ready

Revisions

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