/**
* Reverse a string using common built‑in methods.
* The process:
* - Convert the string into an array of characters.
* - Reverse the array.
* - Join it back into a string.
*/
function reverseString(str) {
return str.split('').reverse().join('');
}
/**
* Measure how long a function takes to run.
* This uses console timers, which are convenient for quick checks.
*
* @param {Function} fn The function to execute.
* @param {...any} args Arguments passed to the function.
* @returns {any} The function's return value.
*/
function time(fn, ...args) {
console.time("timer"); // Start a named timer
const result = fn(...args); // Run the function and capture its output
console.timeEnd("timer"); // Stop the timer and print the duration
return result; // Return the function's result
}
// Run the timing utility with a sample input
time(reverseString, "A String");
/*
run:
timer: 0.022ms
*/