Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Classic Permute vs Johnson-Trotter Permute
(version: 0)
Comparing performance of:
Classic vs Johnson Trotter
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Classic
function permuteSlice(arr) { function generate(input, output, permutations) { if (input.length === 0) { permutations.push(output.slice()); } for (let i = 0; i < input.length; i++) { let newInput = input.slice(0, i).concat(input.slice(i + 1)); output.push(input[i]); generate(newInput, output, permutations); output.pop(); } return permutations; } return generate(arr, [], []); } permuteSlice([1, 2, 3, 4, 5])
Johnson Trotter
function johnsonTrotterPermute(arr) { const permutations = []; function generate(n, input) { if (n === 1) { permutations.push([...input]); return; } generate(n - 1, input); // Recursively generate permutations for n - 1 for (let i = 0; i < n - 1; i++) { // Decide which two elements to swap based on the parity of n if (n % 2 === 0) { [input[i], input[n - 1]] = [input[n - 1], input[i]]; } else { [input[0], input[n - 1]] = [input[n - 1], input[0]]; } generate(n - 1, input); } } generate(arr.length, arr); return permutations; } johnsonTrotterPermute([1, 2, 3, 4, 5])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Classic
Johnson Trotter
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
immutable perf
Spread operator vs. Array.map
Array.prototype.map() VS Spread
Spread operator vs array.map
Comments
Confirm delete:
Do you really want to delete benchmark?