Splitting and trimming

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
No-op with nested trims
const eventNames = "press".trim();
const splits = eventNames.split(",");

// Give the benchmark some work to do so that
// it doesn't optimise out the for loop
// contents
let acc = "";
for(let i = 0; i < splits.length; i++){
	const eventName = splits[i].trim();
	acc += eventName;
}
ready
No-op with regex
const splits = "press".trim().split(/\s*,\s*/);

// Give the benchmark some work to do so that
// it doesn't optimise out the for loop
// contents
let acc = "";
for(let i = 0; i < splits.length; i++){
	const eventName = splits[i];
	acc += eventName;
}
ready
Workful with nested trims
const eventNames = "press,close".trim();
const splits = eventNames.split(",");

// Give the benchmark some work to do so that
// it doesn't optimise out the for loop
// contents
let acc = "";
for(let i = 0; i < splits.length; i++){
	const eventName = splits[i].trim();
	acc += eventName;
}
ready
Workful with regex
const splits = "press,close".trim().split(/\s*,\s*/);

// Give the benchmark some work to do so that
// it doesn't optimise out the for loop
// contents
let acc = "";
for(let i = 0; i < splits.length; i++){
	const eventName = splits[i];
	acc += eventName;
}
ready
Absurd with nested trims
const eventNames = " press , close ".trim();
const splits = eventNames.split(",");

// Give the benchmark some work to do so that
// it doesn't optimise out the for loop
// contents
let acc = "";
for(let i = 0; i < splits.length; i++){
	const eventName = splits[i].trim();
	acc += eventName;
}
ready
Absurd with regex
const splits = " press , close ".trim().split(/\s*,\s*/);

// Give the benchmark some work to do so that
// it doesn't optimise out the for loop
// contents
let acc = "";
for(let i = 0; i < splits.length; i++){
	const eventName = splits[i];
	acc += eventName;
}
ready

Revisions

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