number swap perf test

Benchmark created on


Description

array swap technique vs sum swap

Test runner

Ready to run.

Testing in
TestOps/sec
array swap ES6
let arr = Array.from({length:99999}, (_,index) => index+1)
let start = 0
let end = arr.length-1
while(start < end) {
	[arr[start], arr[end]] = [arr[end], arr[start]];
	++start;
	--end;
}
console.log(arr)
ready
sum swap without third variable
let arr = Array.from({length:99999}, (_,index) => index+1)
let start = 0
let end = arr.length-1
while(start < end) {
	arr[start] = arr[start] + arr[end]
	arr[end] = arr[start] - arr[end]
	arr[start] = arr[start] - arr[end]
	++start;
	--end;
}
console.log(arr)
ready

Revisions

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