Reverse string

Benchmark created on


Setup

let c1 = 'long'
for(let i = 0; i < 12; i++) {
	c1 += c1
}

const CASES = [c1]


Test runner

Ready to run.

Testing in
TestOps/sec
Destructure
function reverse(s) {
	return [...s].reverse().join("")
}

reverse(c1)
ready
for-based swap
function reverse(s) {
	const n = s.length
	const pivot = Math.floor(n / 2)
	const isEven = n % 2
	const result = Array(c1.length)
	
	for(let i=0; i < pivot; i++) {
		result[i] = s[n-i-1]
		result[n-i-1] = s[i]
	}
	
	if (!isEven) {
		result[pivot] = s[pivot]
	}
	return result.join("")
}

reverse(c1)
ready

Revisions

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